<?php

namespace App\Http\Controllers;

use App\Models\Evangelisation;
use App\Models\Rapport_converti_annee;
use Carbon\Carbon;
use Illuminate\Http\Request;


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

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

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

        $nbreM = Evangelisation::whereYear('date', Carbon::now()
            ->year)
            ->where('isConverti', true)
            ->where('isConverti', true)
            ->where('sexe', 'M')
            ->count();

        $nbreF = Evangelisation::whereYear('date', Carbon::now()
            ->year)
            ->where('isConverti', true)
            ->where('sexe', 'F')
            ->count();

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

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

        return $rapport;
    }

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

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $req,  $id)
    {
        $rapport = Rapport_converti_annee::find($id);
        if (!$rapport) {
            return response()->json(['message' => 'Rapport Converti l_an not found'], 404);
        }
        $nbreM = Evangelisation::whereYear('date', Carbon::now()
            ->year)
            ->where('isConverti', true)
            ->where('sexe', 'M')
            ->count();

        $nbreF = Evangelisation::whereYear('date', Carbon::now()
            ->year)
            ->where('isConverti', true)
            ->where('sexe', 'F')
            ->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_converti_annee::find($id);
        if (!$rapport) {
            return response()->json(['message' => 'Rapport Converti l_an not found'], 404);
        }
        $rapport->delete();
        return ['message' => 'Rapport Converti l_an deleted'];
    }
}
