<?php

namespace App\Http\Controllers;

use App\Models\Bapteme;
use App\Models\Rapport_sainte_cene;
use Illuminate\Http\Request;


class RapportSainteCeneController extends Controller
{
    public function index()
    {
        $dataRapport = Rapport_sainte_cene::all();
        if ($dataRapport->isEmpty()) {
            return response()->json(['message' => 'No Rapport_sainte_cene data found in Database'], 404);
        }

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

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $req)
    {
        try {
            $nbre_adulte_Marie_h = Bapteme::whereHas('personne', function ($query) {
                $query->where('sexe', 'M');
            })->whereHas('personne', function ($query) {
                $query->where('isMarried', true);
            })->count();
            $nbre_adulte_Marie_f = Bapteme::whereHas('personne', function ($query) {
                $query->where('sexe', 'F');
            })->whereHas('personne', function ($query) {
                $query->where('isMarried', true);
            })->count();

            $nbre_adulte_Veuf_h = Bapteme::whereHas('personne', function ($query) {
                $query->where('sexe', 'M');
            })->whereHas('personne', function ($query) {
                $query->where('isWidow', true);
            })->count();
            $nbre_adulte_Veuf_f = Bapteme::whereHas('personne', function ($query) {
                $query->where('sexe', 'F');
            })->whereHas('personne', function ($query) {
                $query->where('isWidow', true);
            })->count();

            $nbre_jeune_h = Bapteme::whereHas('personne', function ($query) {
                $query->where('sexe', 'M');
            })->whereHas('personne', function ($query) {
                $query->where('isMarried', false);
            })->count();
            $nbre_jeune_f = Bapteme::whereHas('personne', function ($query) {
                $query->where('sexe', 'F');
            })->whereHas('personne', function ($query) {
                $query->where('isMarried', false);
            })->count();


            $nbre_enfant_h = Bapteme::whereHas('personne', function ($query) {
                $query->where('sexe', 'M');
            })->whereHas('personne', function ($query) {
                $query->where('age',  '<', 15);
            })->count();
            $nbre_enfant_f = Bapteme::whereHas('personne', function ($query) {
                $query->where('sexe', 'F');
            })->whereHas('personne', function ($query) {
                $query->where('age',  '<', 15);
            })->count();

            $nbre_all_total_h = $nbre_adulte_Marie_h
                + $nbre_adulte_Veuf_h  + $nbre_jeune_h  + $nbre_enfant_h;
            $nbre_all_total_f =  $nbre_adulte_Marie_f
                + $nbre_adulte_Veuf_f +  $nbre_jeune_f
                + $nbre_enfant_f;
            $nbre_all_total = $nbre_adulte_Marie_h + $nbre_adulte_Marie_f
                + $nbre_adulte_Veuf_h + $nbre_adulte_Veuf_f + $nbre_jeune_h + $nbre_jeune_f + $nbre_enfant_h
                + $nbre_enfant_f;

            $req->merge([
                'nbre_adulte_Marie_h' =>  $nbre_adulte_Marie_h,
                'nbre_adulte_Marie_f' =>  $nbre_adulte_Marie_f,
                'nbre_adulte_Veuf_h' =>  $nbre_adulte_Veuf_h,
                'nbre_adulte_Veuf_f' =>  $nbre_adulte_Veuf_f,

                'nbre_jeune_h' =>  $nbre_jeune_h,
                'nbre_jeune_f' =>  $nbre_jeune_f,
                'nbre_enfant_h' =>  $nbre_enfant_h,
                'nbre_enfant_f' =>  $nbre_enfant_f,
                'nbre_all_total_h' =>  $nbre_all_total_h,
                'nbre_all_total_f' =>  $nbre_all_total_f,
                'nbre_all_total' =>  $nbre_all_total
            ]);
            $dataRapport = Rapport_sainte_cene::create($req->all());
            return $dataRapport;
        } catch (\Throwable $th) {
            return $th;
        }
    }

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

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

        $nbre_adulte_Marie_h = Bapteme::whereHas('personne', function ($query) {
            $query->where('sexe', 'M');
        })->whereHas('personne', function ($query) {
            $query->where('isMarried', true);
        })->count();
        $nbre_adulte_Marie_f = Bapteme::whereHas('personne', function ($query) {
            $query->where('sexe', 'F');
        })->whereHas('personne', function ($query) {
            $query->where('isMarried', true);
        })->count();

        $nbre_adulte_Veuf_h = Bapteme::whereHas('personne', function ($query) {
            $query->where('sexe', 'M');
        })->whereHas('personne', function ($query) {
            $query->where('isWidow', true);
        })->count();
        $nbre_adulte_Veuf_f = Bapteme::whereHas('personne', function ($query) {
            $query->where('sexe', 'F');
        })->whereHas('personne', function ($query) {
            $query->where('isWidow', true);
        })->count();

        $nbre_jeune_h = Bapteme::whereHas('personne', function ($query) {
            $query->where('sexe', 'M');
        })->whereHas('personne', function ($query) {
            $query->where('isMarried', false);
        })->count();
        $nbre_jeune_f = Bapteme::whereHas('personne', function ($query) {
            $query->where('sexe', 'F');
        })->whereHas('personne', function ($query) {
            $query->where('isMarried', false);
        })->count();


        $nbre_enfant_h = Bapteme::whereHas('personne', function ($query) {
            $query->where('sexe', 'M');
        })->whereHas('personne', function ($query) {
            $query->where('age',  '<', 15);
        })->count();
        $nbre_enfant_f = Bapteme::whereHas('personne', function ($query) {
            $query->where('sexe', 'F');
        })->whereHas('personne', function ($query) {
            $query->where('age',  '<', 15);
        })->count();

        $nbre_all_total_h = $nbre_adulte_Marie_h
            + $nbre_adulte_Veuf_h  + $nbre_jeune_h  + $nbre_enfant_h;
        $nbre_all_total_f =  $nbre_adulte_Marie_f
            + $nbre_adulte_Veuf_f +  $nbre_jeune_f
            + $nbre_enfant_f;
        $nbre_all_total = $nbre_adulte_Marie_h + $nbre_adulte_Marie_f
            + $nbre_adulte_Veuf_h + $nbre_adulte_Veuf_f + $nbre_jeune_h + $nbre_jeune_f + $nbre_enfant_h
            + $nbre_enfant_f;

        $req->merge([
            'nbre_adulte_Marie_h' =>  $nbre_adulte_Marie_h,
            'nbre_adulte_Marie_f' =>  $nbre_adulte_Marie_f,
            'nbre_adulte_Veuf_h' =>  $nbre_adulte_Veuf_h,
            'nbre_adulte_Veuf_f' =>  $nbre_adulte_Veuf_f,

            'nbre_jeune_h' =>  $nbre_jeune_h,
            'nbre_jeune_f' =>  $nbre_jeune_f,
            'nbre_enfant_h' =>  $nbre_enfant_h,
            'nbre_enfant_f' =>  $nbre_enfant_f,
            'nbre_all_total_h' =>  $nbre_all_total_h,
            'nbre_all_total_f' =>  $nbre_all_total_f,
            'nbre_all_total' =>  $nbre_all_total
        ]);

        $dataRapport->update($req->all());
        return $dataRapport;
    }

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

        return [
            'message' => "Rapport_sainte_cene deleted"
        ];
    }
}
