<?php

namespace App\Metier ;

/*******************
*                  *
* Created By S4M37 *
*                  *
********************/


use App\Models\Session;
use App\Models\Examen;
use App\Models\Station;
use App\Models\Item;
use App\Models\Banque;
use App\Models\Note_Examen;
use App\Models\Note_Station;
use App\Models\Note_Item;

class GestNotes
{

	function createNoteExamen($id_Examen,$id_Session,$id_Etudiant)
	{
		$note_Examen = Note_Examen::whereIdExamenAndIdSessionAndIdEtudiant($id_Examen,$id_Session,$id_Etudiant)->first();
		if(!$note_Examen)
		{
			$note_Examen=new Note_Examen;
			$note_Examen->id_Examen=$id_Examen;
			$note_Examen->id_Session=$id_Session;
			$note_Examen->id_Etudiant=$id_Etudiant;
			$note_Examen->note=0;

			$note_Examen->save();
			$note_Examen = Note_Examen::whereIdExamenAndIdSessionAndIdEtudiant($id_Examen,$id_Session,$id_Etudiant)->first();
		}
		return $note_Examen->id_Note_Examen;
	}

	function createNoteStation($id_Note_Examen,$id_Station)
	{
		$note_Station = Note_Station::whereIdNoteExamenAndIdStation($id_Note_Examen,$id_Station)->first();
		if(!$note_Station)
		{
			$note_Station=new Note_Station;
			$note_Station->id_Note_Examen=$id_Note_Examen;
			$note_Station->id_Station=$id_Station;
			$note_Station->note=0;

			$note_Station->save();
			$note_Station = Note_Station::whereIdNoteExamenAndIdStation($id_Note_Examen,$id_Station)->first();
		}
		return $note_Station->id_Note_Station;
	}

	function createItemNote($id_Item,$id_Note_Station,$note)
	{
		$note_Item= new Note_Item;
		$note_Item->id_Note_Station=$id_Note_Station;
		$note_Item->id_Item=$id_Item;
		$note_Item->note=$note;
		$note_Item->save();
	}

	function attributeNote($id_Station,$id_Etudiant,Array $inputs)
	{
		$id_Examen=Station::whereIdStation($id_Station)->first()->id_Examen;
		$id_Session=Examen::whereIdExamen($id_Examen)->first()->id_Session;
		$id_Note_Examen=$this->createNoteExamen($id_Examen,$id_Session,$id_Etudiant);
		$id_Note_Station=$this->createNoteStation($id_Note_Examen,$id_Station);
		$id_Banque=Station::whereIdStation($id_Station)->first()->id_Banque;
		$items=Item::whereIdBanque($id_Banque)->get();
		if(count($items)!=count($inputs))
		{
			return response()->json(['error' => 'Bad request'], HttpResponse::HTTP_BAD_REQUEST);
		} else {


			$i=0;
			foreach ($items as $item) 
			{
				$this->createItemNote($item->id_Item,$id_Note_Station, $inputs[$i]);
				$i++;
			}
			return "";
		}
	}

	function calculeNoteExamen($id_Examen)
	{
		$note_Examens=Note_Examen::whereIdExamen($id_Examen)->get();
		foreach ($note_Examens as $note_Examen) 
		{
			$note_Examen_Note=0;
			$note_Stations=Note_Station::whereIdNoteExamen($note_Examen->id_Note_Examen)->get();
			foreach ($note_Stations as $note_Station) 
			{
				$note_Items=Note_Item::whereIdNoteStation($note_Station->id_Note_Station)->get();
				$note_Station_Note=0;
				foreach ($note_Items as $note_Item) 
				{
					if($note_Item->note==1)
					{
						$note_Station_Note+=Item::whereIdItem($note_Item->id_Item)->first()->valeur;
					}
				}
				$note_Station->note=$note_Station_Note;
				$note_Station->save();
				$note_Examen_Note+=($note_Station_Note*(Station::whereIdStation($note_Station->id_Station)
				->first()->ponderation))/100;
			}
			$note_Examen->note=$note_Examen_Note;
			$note_Examen->save();				
		}
	}
	
	function getExamenNotes($sessionId,$examenId){
		return Note_Examen::whereIdSessionAndIdExamen($sessionId,$examenId)
                ->join('Etudiant','Etudiant.id_Etudiant','=','Note_Examen.id_Etudiant')
                ->get();
	}
	
	function getExamenNote($examenNoteId){
		return Note_Examen::find($examenNoteId);
	}
	
	function getNoteItems($stationNoteId){
		return Note_Item::join('Item','Item.id_Item','=','Note_Item.id_Item')
                ->whereIdNoteStation($stationNoteId)->get();
	}
	
	function getNoteItem($itemNoteId){
		return Note_Item::find($itemNoteId);
	}
	
	function getNoteStations($examenNoteId){
		return Note_Station::whereIdNoteExamen($examenNoteId)
                ->join('Station','Station.id_Station','=','Note_Station.id_Station')
                ->join('Banque','Banque.id_Banque','=','Station.id_Banque')->get();
	}

	function getNoteStation($stationNoteId){
		return Note_Station::find($stationNoteId);
	}
}