<?php

namespace App\Http\Controllers;

use App\Models\Dece;
use App\Models\Rapport_dece_annee;
use Carbon\Carbon;
use Illuminate\Http\Request;


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

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

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

        $nbre_adulte_Marie_h = Dece::whereYear('date', Carbon::now()
            ->year)
            ->whereHas('personne', function ($query) {
                $query->where('sexe', 'M');
            })
            ->whereHas('personne', function ($query) {
                $query->where('isDead', true);
            })
            ->whereHas('personne', function ($query) {
                $query->where('isMarried', true);
            })
            ->count();
        $nbre_adulte_Marie_f = Dece::whereYear('date', Carbon::now()
            ->year)
            ->whereHas('personne', function ($query) {
                $query->where('isDead', true);
            })
            ->whereHas('personne', function ($query) {
                $query->where('isMarried', true);
            })
            ->whereHas('personne', function ($query) {
                $query->where('sexe', 'F');
            })
            ->count();

        $nbre_adulte_Veuf_h = Dece::whereYear('date', Carbon::now()
            ->year)->whereHas('personne', function ($query) {
            $query->where('isDead', true);
        })->whereHas('personne', function ($query) {
            $query->where('isWidow', true);
        })->whereHas('personne', function ($query) {
            $query->where('sexe', 'M');
        })->count();
        $nbre_adulte_Veuf_f = Dece::whereYear('date', Carbon::now()
            ->year)->whereHas('personne', function ($query) {
            $query->where('isDead', true);
        })->whereHas('personne', function ($query) {
            $query->where('isWidow', true);
        })->whereHas('personne', function ($query) {
            $query->where('sexe', 'F');
        })
            ->count();

        $nbre_adulte_Divorce_h = Dece::whereYear('date', Carbon::now()
            ->year)->whereHas('personne', function ($query) {
            $query->where('isDead', true);
        })->whereHas('personne', function ($query) {
            $query->where('isDivorce', true);
        })->whereHas('personne', function ($query) {
            $query->where('sexe', 'M');
        })
            ->count();


        $nbre_jeune_h = Dece::whereYear('date', Carbon::now()
            ->year)->whereHas('personne', function ($query) {
            $query->where('isDead', true);
        })->whereHas('personne', function ($query) {
            $query->where('isMarried', false);
        })->whereHas('personne', function ($query) {
            $query->where('sexe', 'M');
        })
            ->count();

        $nbre_jeune_f = Dece::whereYear('date', Carbon::now()
            ->year)->whereHas('personne', function ($query) {
            $query->where('isDead', true);
        })->whereHas('personne', function ($query) {
            $query->where('isMarried', false);
        })->whereHas('personne', function ($query) {
            $query->where('sexe', 'F');
        })
            ->count();

        $nbre_enfant_h = Dece::whereYear('date', Carbon::now()
            ->year)->whereHas('personne', function ($query) {
            $query->where('isDead', true);
        })->whereHas('personne', function ($query) {
            $query->where('age', '<', 15);
        })->whereHas('personne', function ($query) {
            $query->where('sexe', 'M');
        })
            ->count();
        $nbre_enfant_f = Dece::whereYear('date', Carbon::now()
            ->year)->whereHas('personne', function ($query) {
            $query->where('isDead', true);
        })->whereHas('personne', function ($query) {
            $query->where('age', '<', 15);
        })->whereHas('personne', function ($query) {
            $query->where('sexe', 'F');
        })
            ->count();
        $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' =>  $nbre_all_total
        ]);
        try {
            $dataRapport = Rapport_dece_annee::create($req->all());
            return $dataRapport;
        } catch (\Throwable $th) {
            return $th;
        }
    }

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

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

        $nbre_adulte_Marie_h = Dece::whereYear('date', Carbon::now()
            ->year)
            ->whereHas('personne', function ($query) {
                $query->where('sexe', 'M');
            })
            ->whereHas('personne', function ($query) {
                $query->where('isDead', true);
            })
            ->whereHas('personne', function ($query) {
                $query->where('isMarried', true);
            })
            ->count();
        $nbre_adulte_Marie_f = Dece::whereYear('date', Carbon::now()
            ->year)
            ->whereHas('personne', function ($query) {
                $query->where('isDead', true);
            })
            ->whereHas('personne', function ($query) {
                $query->where('isMarried', true);
            })
            ->whereHas('personne', function ($query) {
                $query->where('sexe', 'F');
            })
            ->count();

        $nbre_adulte_Veuf_h = Dece::whereYear('date', Carbon::now()
            ->year)->whereHas('personne', function ($query) {
            $query->where('isDead', true);
        })->whereHas('personne', function ($query) {
            $query->where('isWidow', true);
        })->whereHas('personne', function ($query) {
            $query->where('sexe', 'M');
        })->count();
        $nbre_adulte_Veuf_f = Dece::whereYear('date', Carbon::now()
            ->year)->whereHas('personne', function ($query) {
            $query->where('isDead', true);
        })->whereHas('personne', function ($query) {
            $query->where('isWidow', true);
        })->whereHas('personne', function ($query) {
            $query->where('sexe', 'F');
        })
            ->count();

        $nbre_adulte_Divorce_h = Dece::whereYear('date', Carbon::now()
            ->year)->whereHas('personne', function ($query) {
            $query->where('isDead', true);
        })->whereHas('personne', function ($query) {
            $query->where('isDivorce', true);
        })->whereHas('personne', function ($query) {
            $query->where('sexe', 'M');
        })
            ->count();

        $nbre_adulte_Divorce_f = Dece::whereYear('date', Carbon::now()
            ->year)->whereHas('personne', function ($query) {
            $query->where('isDead', true);
        })->whereHas('personne', function ($query) {
            $query->where('isDivorce', true);
        })->whereHas('personne', function ($query) {
            $query->where('sexe', 'F');
        })
            ->count();

        $nbre_jeune_h = Dece::whereYear('date', Carbon::now()
            ->year)->whereHas('personne', function ($query) {
            $query->where('isDead', true);
        })->whereHas('personne', function ($query) {
            $query->where('isMarried', false);
        })->whereHas('personne', function ($query) {
            $query->where('sexe', 'M');
        })
            ->count();

        $nbre_jeune_f = Dece::whereYear('date', Carbon::now()
            ->year)->whereHas('personne', function ($query) {
            $query->where('isDead', true);
        })->whereHas('personne', function ($query) {
            $query->where('isMarried', false);
        })->whereHas('personne', function ($query) {
            $query->where('sexe', 'F');
        })
            ->count();

        $nbre_enfant_h = Dece::whereYear('date', Carbon::now()
            ->year)->whereHas('personne', function ($query) {
            $query->where('isDead', true);
        })->whereHas('personne', function ($query) {
            $query->where('age', '<', 15);
        })->whereHas('personne', function ($query) {
            $query->where('sexe', 'M');
        })
            ->count();
        $nbre_enfant_f = Dece::whereYear('date', Carbon::now()
            ->year)->whereHas('personne', function ($query) {
            $query->where('isDead', true);
        })->whereHas('personne', function ($query) {
            $query->where('age', '<', 15);
        })->whereHas('personne', function ($query) {
            $query->where('sexe', 'F');
        })
            ->count();
        $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' =>  $nbre_all_total
        ]);

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

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

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