<?php

namespace App\Http\Controllers;

use App\Models\Commission;
use App\Models\Evangelisation;
use App\Models\Ouvrage_livre;
use App\Models\Rapport_discipolat;
use App\Models\Serviteur;
use Carbon\Carbon;
use Illuminate\Http\Request;


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

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

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

            $nbreGroupe = Commission::count();
            $nbrePersSw = Serviteur::count();
            $nbrePersFr = Serviteur::count();
            $nbreLivreSw = Ouvrage_livre::where('langue', 'Sw')
                ->count();
            $nbreLivreFr = Ouvrage_livre::where('langue', 'Fr')
                ->count();


            $req->merge([
                'nbre_groupe' =>  $nbreGroupe,
                'nbre_personne_sw' =>  $nbrePersSw,
                'nbre_personne_fr' =>  $nbrePersFr,
                'nbre_livre_sw' =>  $nbreLivreSw,
                'nbre_livre_fr' =>  $nbreLivreFr,
            ]);

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

            return $rapport;
        } catch (\Throwable $th) {
            return $th;
        }
    }

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

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $req,  $id)
    {
        $rapport = Rapport_discipolat::find($id);
        if (!$rapport) {
            return response()->json(['message' => 'Rapport Discipolat not found'], 404);
        }
        $nbreGroupe = Commission::count();
        $nbrePersSw = Serviteur::count();
        $nbrePersFr = Serviteur::count();
        $nbreLivreSw = Ouvrage_livre::where('langue', 'Sw')
            ->count();
        $nbreLivreFr = Ouvrage_livre::where('langue', 'Fr')
            ->count();


        $req->merge([
            'nbre_groupe' =>  $nbreGroupe,
            'nbre_personne_sw' =>  $nbrePersSw,
            'nbre_personne_fr' =>  $nbrePersFr,
            'nbre_livre_sw' =>  $nbreLivreSw,
            'nbre_livre_fr' =>  $nbreLivreFr,
        ]);

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

        return $rapport;
    }

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