<?php

namespace App\Jobs;

use App\Models\Classe;
use App\Models\LevelMatter;
use App\Models\MoyenneMatiere;
use App\Models\CuttingSchoolYear;
use App\Events\MoyenneMatiereEvent;
use App\Models\MoyenneDecoupageMatiere;
use App\Jobs\BonnusMoyenneAnnuelleMatiereJob;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class BonnusMoyenneMatiereJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $student, $matter, $cutting, $classe, $approved;

    public function __construct($student, $matter, $cutting, $classe = null, $approved)
    {
        $this->student = $student;
        $this->matter = $matter;
        $this->cutting = $cutting;
        $this->classe = $classe;
        $this->approved = $approved;
    }

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        $cutting = CuttingSchoolYear::find($this->cutting);
        foreach(getClassement($this->student) as $item){
            $exist = MoyenneMatiere::where('inscription_id', $item['id'])->where('level_matter_id', $this->matter)->where('cutting_school_year_id', $this->cutting)->first();
            if($exist){
                $exist->update(['moyenne' => $item['moyen'], 'rang' => $item['rang'] ]);
            }
            else{
                $this->saveMoyen($item['id'], $item['moyen'], $item['rang']);
            }
            
            $this->approved ? $this->updateMoyen($item['id'], $item['moyen'], $item['rang']):null;

            // Déclenchement de l’événement
            event(new MoyenneMatiereEvent($item['id'], $item['moyen'], $item['rang'], $this->getCoef(), $cutting->school_year_id, $cutting->cutting->libelle, $this->matter, $this->school()));
        }

        // Déclenchement de Job pour le calcul des moyennes Annuelles .................
        if($cutting->cutting->end != 'oui'){
            BonnusMoyenneAnnuelleMatiereJob::dispatch($this->student, $this->matter, $cutting->school_year_id)->delay(now()->addSeconds(1));
        }
    }



    private function getCoef()
    {
        $val = LevelMatter::find($this->matter);
        return $val->coefficient->valeur;
    }


    private function school(){
        $class = Classe::find($this->classe);
        return $class->school_id;
    }


    private function saveMoyen($student, $moyen, $rang){
        MoyenneMatiere::create([
            'inscription_id' => $student,
            'moyenne' => $moyen,
            'rang' => $rang,
            'level_matter_id' => $this->matter,
            'cutting_school_year_id' => $this->cutting
        ]);
    }

    private function updateMoyen($student, $moyen, $rang)
    {
        $val = MoyenneDecoupageMatiere::where('inscription_id', $student)->where('level_matter_id', $this->matter)->where('cutting_school_year_id', $this->cutting)->first();
        if($val) {
            $val->update([
                'moyenne' => $moyen,
                'rang' => $rang
            ]);
        }
        else{
            MoyenneDecoupageMatiere::create([
                'inscription_id' => $student,
                'moyenne' => $moyen,
                'rang' => $rang,
                'level_matter_id' => $this->matter,
                'cutting_school_year_id' => $this->cutting
            ]); 
        }
    }
}