<?php

namespace App\Jobs;

use App\Models\Matter;
use App\Models\LevelMatter;
use App\Models\MoyenneMatiere;
use App\Models\MoyenneDecoupageMatiere;
use App\Jobs\CalculMoyenneTrimestielleJob;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Bus;

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

    protected $classe, $matter, $cutting, $school;
    public function __construct($classe, $matter, $cutting, $school)
    {
        $this->classe = $classe;
        $this->matter = $matter;
        $this->cutting = $cutting;
        $this->school = $school;
    }

    public function handle(): void
    {
        $data = $this->getMoyenMatter();
        foreach($data as $item){
            $val = $this->verifyMoyen($item['id']);
            if($val){
                $val->update([
                    'moyenne' => $item['moyenne'],
                    'rang' => $item['rang']
                ]);
            }
            else{
                $this->saveMoyen($item['id'], $item['moyenne'], $item['rang']);
            }
        }

        // Déclenchement des autres jobs
        Bus::chain([ new CalculMoyenneTrimestielleJob($this->classe, $this->cutting, $this->bilan()) ])->dispatch();
    }


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


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


    private function getMoyenMatter() {
        $data = MoyenneMatiere::join('inscriptions', 'moyenne_matieres.inscription_id', '=', 'inscriptions.id')
        ->select('moyenne_matieres.moyenne', 'moyenne_matieres.rang', 'inscriptions.id')
        ->where('moyenne_matieres.cutting_school_year_id', $this->cutting)
        ->where('moyenne_matieres.level_matter_id', $this->matter)
        ->where('inscriptions.classe_id', $this->classe)->get();
        return json_decode($data, true);
    }


    private function verifyMoyen($item) {
        $val = MoyenneDecoupageMatiere::where('inscription_id', $item)->where('level_matter_id', $this->matter)->where('cutting_school_year_id', $this->cutting)->first();
        return $val ?? null;
    }


    private function bilan() {
        $dts = Matter::join('level_matter', 'matters.id', '=', 'level_matter.matter_id')
        ->join('libelle_bilan_matter', 'matters.id', '=', 'libelle_bilan_matter.matter_id')
        ->select('libelle_bilan_matter.libelle_bilan_id')
        ->where('level_matter.id', '=', $this->matter)
        ->first();
        return $dts ? $dts->libelle_bilan_id:null;
    }
}
