<?php

namespace App\Jobs;

use App\Models\DisctinctMoyenne;
use App\Models\DistinctCuttingActif;
use App\Models\MoyenneTrimestrielle;
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\DB;

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

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

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        $dts = getClassement($this->getStudentMoyenne());
        DisctinctMoyenne::where('school_id', $this->school)->where('cutting_school_year_id', $this->cutting)->delete();
        foreach($dts as $item){
            DisctinctMoyenne::create([
                'rang' => $item['rang'],
                'moyenne' => $item['moyen'],
                'school_id' => $this->school,
                'inscription_id' => $item['id'],
                'mension' => $this->getMension($item['moyen']),
                'cutting_school_year_id' => $this->cutting
            ]);
        }
        $this->getCutting();
    }


    private function getMoyenneStudent(){

        $dts = DB::table('students')
        ->join('inscriptions', 'students.id', '=', 'inscriptions.student_id')
        ->join('moyenne_trimestrielles', 'inscriptions.id', '=', 'moyenne_trimestrielles.inscription_id')
        ->join('moyenne_decoupage_matieres', 'inscriptions.id', '=', 'moyenne_decoupage_matieres.inscription_id')
        ->select('students.first_name', 'students.last_name', 'students.sexe', 'inscriptions.id')
        ->where('inscriptions.school_id', '=', $this->school)
        ->where('moyenne_trimestrielles.cutting_school_year_id', '=', $this->cutting)
        ->where('moyenne_trimestrielles.moyenne', '>=', 12)
        ->groupBy('moyenne_decoupage_matieres.inscription_id')
        ->havingRaw('MIN(moyenne_decoupage_matieres.moyenne) >= 10')
        ->orderBy('students.first_name')->orderBy('students.last_name')
        ->get();
        return $dts;
    }


    private function getStudentMoyenne(){
        $dts = $this->getMoyenneStudent();
        $table = [];
        foreach($dts as $item){
            $moyen = MoyenneTrimestrielle::where('inscription_id', $item->id)->where('cutting_school_year_id', $this->cutting)->first();
            $table[] = [
                'id' => $item->id,
                'sexe' => $item->sexe,
                'moyen' => $moyen['moyenne']
            ];
        }
        return $table;
    }

    private Function getMension($val){
        if($val >= 12 && $val < 13 ){
            $valeur = 1; // Tableau d\'honneur
        }
        elseif($val >= 13 && $val < 14){
            $valeur = 2; // Tableau d\'honneur + Encouragement
        }
        elseif($val >= 14){
            $valeur = 3;  // Tableau d\'honneur + Félicitation
        }
        return $valeur;
    }

    private function getCutting(){
        $count = DisctinctMoyenne::where('cutting_school_year_id', $this->cutting)->count();
        if($count){
            $exist = DistinctCuttingActif::where('cutting_school_year_id', $this->cutting)->first();
            if(!$exist){
                DistinctCuttingActif::where('actif', '1')->update(['actif' => '0']); // Désactiver les autres ----

                DistinctCuttingActif::create([
                    'cutting_school_year_id' =>$this->cutting
                ]);
            }
        }
    }
}
