<?php

namespace App\Jobs;

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

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

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


    public function handle(): void
    {
        $data = getClassement($this->student);
        foreach($data as $item){
            $exist = $this->verifyMoyen($item['id']);
            if($exist){
                $exist->update(['rang' => $item['rang'], 'moyenne' => $item['moyen']]);
            }
            else{
                $this->saveMoyen($item['id'], $item['moyen'], $item['rang']);
            }
            $this->approved ? $this->updateMoyen($item['id'], $item['moyen'], $item['rang']):null;
        }

        if($this->approved) {
            Bus::chain([
                new CalculMoyenneTrimestielleJob($this->classe, $this->cutting, $this->bilan())
            ])->dispatch();
        }
    }


    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($item, $moyen, $rang) {
        MoyenneMatiere::create([
            'rang' => $rang,
            'moyenne' => $moyen,
            'inscription_id' => $item,
            'level_matter_id' => $this->matter,
            'cutting_school_year_id' => $this->cutting
        ]);
    }


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


    private function verifyMoyen($item) {
        $val = MoyenneMatiere::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;
    }
}