<?php

namespace App\Http\Controllers;

use App\Models\Personne;
use App\Models\Rapport_retrograde;
use Illuminate\Http\Request;


class RapportRetrogradeController extends Controller
{

    public function index()
    {
        $dataRapport = Rapport_retrograde::all();
        if ($dataRapport->isEmpty()) {
            return response()->json(['message' => 'No Rapport_retrograde 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 = Personne::where('isRetrograde', true)
                ->where('sexe', 'M')
                ->where('isMarried', true)
                ->count();
            $nbre_adulte_Marie_f = Personne::where('isRetrograde', true)
                ->where('sexe', 'F')
                ->where('isMarried', true)
                ->count();
            $nbre_adulte_Veuf_h = Personne::where('isRetrograde', true)
                ->where('sexe', 'M')
                ->where('isWidow', true)
                ->count();
            $nbre_adulte_Veuf_f = Personne::where('isRetrograde', true)
                ->where('sexe', 'F')
                ->where('isWidow', true)
                ->count();

            $nbre_jeune_h =  Personne::where('isRetrograde', true)
                ->where('sexe', 'M')
                ->where('isMarried', true)
                ->count();
            $nbre_jeune_f =  Personne::where('isRetrograde', true)
                ->where('sexe', 'F')
                ->where('isMarried', true)
                ->count();
            $nbre_enfant_h =  Personne::where('isRetrograde', true)
                ->where('sexe', 'M')
                ->where('age', '<', 15)
                ->count();
            $nbre_enfant_f =  Personne::where('isRetrograde', true)
                ->where('sexe', 'F')
                ->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_retrograde::create($req->all());
            return $dataRapport;
        } catch (\Throwable $th) {
            return $th;
        }
    }

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

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $req,  $id)
    {
        $dataRapport = Rapport_retrograde::find($id);
        if (!$dataRapport) {
            return response()->json(['message' => 'Rapport_retrograde not found'], 404);
        }
        $nbre_adulte_Marie_h = Personne::where('isRetrograde', true)
            ->where('sexe', 'M')
            ->where('isMarried', true)
            ->count();
        $nbre_adulte_Marie_f = Personne::where('isRetrograde', true)
            ->where('sexe', 'F')
            ->where('isMarried', true)
            ->count();
        $nbre_adulte_Veuf_h = Personne::where('isRetrograde', true)
            ->where('sexe', 'M')
            ->where('isWidow', true)
            ->count();
        $nbre_adulte_Veuf_f = Personne::where('isRetrograde', true)
            ->where('sexe', 'F')
            ->where('isWidow', true)
            ->count();

        $nbre_jeune_h =  Personne::where('isRetrograde', true)
            ->where('sexe', 'M')
            ->where('isMarried', true)
            ->count();
        $nbre_jeune_f =  Personne::where('isRetrograde', true)
            ->where('sexe', 'F')
            ->where('isMarried', true)
            ->count();
        $nbre_enfant_h =  Personne::where('isRetrograde', true)
            ->where('sexe', 'M')
            ->where('age', '<', 15)
            ->count();
        $nbre_enfant_f =  Personne::where('isRetrograde', true)
            ->where('sexe', 'F')
            ->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_retrograde::find($id);
        if (!$dataRapport) {
            return response()->json(['message' => 'Rapport_retrograde not found'], 404);
        }
        $dataRapport->delete();

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