<?php

namespace App\Http\Controllers;

use App\Models\Handicap;
use App\Models\Rapport_handicap;
use Illuminate\Http\Request;


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

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

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

        $nbreM = Handicap::whereHas('personne', function ($query) {
            $query->where('sexe', 'F');
        })->whereHas('personne', function ($query) {
            $query->where('isHandicap', true);
        })
            ->count();
        $nbreF = Handicap::whereHas('personne', function ($query) {
            $query->where('sexe', 'M');
        })->whereHas('personne', function ($query) {
            $query->where('isHandicap', true);
        })
            ->count();

        $nbreTotal = $nbreM + $nbreF;
        $req->merge([
            'nbre_h' =>  $nbreM,
            'nbre_f' =>  $nbreF,
            'nbre_total' =>  $nbreTotal
        ]);

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

        return $rapport;
    }

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

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $req,  $id)
    {
        $rapport = Rapport_handicap::find($id);
        if (!$rapport) {
            return response()->json(['message' => 'Rapport Handicap not found'], 404);
        }
        $nbreM = Handicap::whereHas('personne', function ($query) {
            $query->where('sexe', 'F');
        })->whereHas('personne', function ($query) {
            $query->where('isHandicap', true);
        })
            ->count();
        $nbreF = Handicap::whereHas('personne', function ($query) {
            $query->where('sexe', 'M');
        })->whereHas('personne', function ($query) {
            $query->where('isHandicap', true);
        })
            ->count();

        $nbreTotal = $nbreM + $nbreF;
        $req->merge([
            'nbre_h' =>  $nbreM,
            'nbre_f' =>  $nbreF,
            'nbre_total' =>  $nbreTotal
        ]);

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

        return $rapport;
    }

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