<?php

namespace App\Http\Controllers;

use App\Models\Rapport_mission;
use App\Models\Responsable_commission;
use App\Models\Serviteur;
use Illuminate\Http\Request;

class RapportMissionController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $dataRapport = Rapport_mission::all();
        if ($dataRapport->isEmpty()) {
            return response()->json(['message' => 'No Rapport Mission Annee data found in Database'], 404);
        }

        return [
            'size' => sizeof($dataRapport),
            'data' => $dataRapport
        ];
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $req)
    {

        $nbreAmisM = Serviteur::whereHas('personne', function ($query) {
            $query->where('sexe', 'M');
        })->count();
        $nbreAmisF = Serviteur::whereHas('personne', function ($query) {
            $query->where('sexe', 'F');
        })->count();
        $nbreComiteM = Responsable_commission::whereHas('commission', function ($query) {
            $query->where('code', '==', "Mission");
        })->whereHas('personne', function ($query) {
            $query->where('sexe', 'M');
        })->count();

        $nbreComiteF = Responsable_commission::whereHas('commission', function ($query) {
            $query->where('code', '==', "Mission");
        })->whereHas('personne', function ($query) {
            $query->where('sexe', 'F');
        })->count();

        $nbreAmisTotal = $nbreAmisM + $nbreAmisF;
        $nbreComiteTotal = $nbreComiteM + $nbreComiteF;
        $req->merge([
            'nbre_amis_h' =>  $nbreAmisM,
            'nbre_amis_f' =>  $nbreAmisF,
            'nbre_amis_total' =>  $nbreAmisTotal,
            'nbre_comite_h' =>  $nbreComiteM,
            'nbre_comite_f' =>  $nbreComiteF,
            'nbre_comite_total' =>  $nbreComiteTotal
        ]);

        $rapport = Rapport_mission::create($req->all());

        return $rapport;
    }

    /**
     * Display the specified resource.
     */
    public function show($id)
    {
        $rapport = Rapport_mission::find($id);
        if (!$rapport) {
            return response()->json(['message' => 'Rapport Mission not found'], 404);
        }
        return response()->json($rapport);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $req,  $id)
    {
        $rapport = Rapport_mission::find($id);
        if (!$rapport) {
            return response()->json(['message' => 'Rapport Mission not found'], 404);
        }

        $nbreAmisM = Serviteur::whereHas('personne', function ($query) {
            $query->where('sexe', 'M');
        })->count();
        $nbreAmisF = Serviteur::whereHas('personne', function ($query) {
            $query->where('sexe', 'F');
        })->count();
        $nbreComiteM = Responsable_commission::whereHas('commission', function ($query) {
            $query->where('code', '==', "Mission");
        })->whereHas('personne', function ($query) {
            $query->where('sexe', 'M');
        })->count();

        $nbreComiteF = Responsable_commission::whereHas('commission', function ($query) {
            $query->where('code', '==', "Mission");
        })->whereHas('personne', function ($query) {
            $query->where('sexe', 'F');
        })->count();

        $nbreAmisTotal = $nbreAmisM + $nbreAmisF;
        $nbreComiteTotal = $nbreComiteM + $nbreComiteF;
        $req->merge([
            'nbre_amis_h' =>  $nbreAmisM,
            'nbre_amis_f' =>  $nbreAmisF,
            'nbre_amis_total' =>  $nbreAmisTotal,
            'nbre_comite_h' =>  $nbreComiteM,
            'nbre_comite_f' =>  $nbreComiteF,
            'nbre_comite_total' =>  $nbreComiteTotal
        ]);

        $rapport->update($req->all());

        return $rapport;
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy($id)
    {
        $rapport = Rapport_mission::find($id);
        if (!$rapport) {
            return response()->json(['message' => 'Rapport Mission not found'], 404);
        }
        $rapport->delete();
        return ['message' => 'Rapport Mission deleted'];
    }
}