<?php

namespace App\Http\Controllers;

use App\Models\Benediction_mariage;
use App\Http\Requests\StoreBenediction_mariageRequest;
use App\Http\Requests\UpdateBenediction_mariageRequest;
use Illuminate\Http\Request;

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

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

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $req)
    {
        $champs = $req->validate([
            'nom_epoux' => 'required|string|max:255',
            'nom_epouse' => 'required|string|max:255',
            'eglise_provenance_epoux' => 'required|string|max:255',
            'eglise_provenance_epouse' => 'required|string|max:255',
            'numero_certificat' => 'required|string|max:255',
            'lieu' => 'required|string|max:255',
            'date' => 'required|date',
        ]);

        $dataMariage = Benediction_mariage::create($champs);
        return $dataMariage;
    }

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

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $req,  $id)
    {
        $dataMariage = Benediction_mariage::find($id);
        if (!$dataMariage) {
            return response()->json(['message' => 'Mariage not found']);
        }
        $champs = $req->validate([
            'nom_epoux' => 'required|string|max:255',
            'nom_epouse' => 'required|string|max:255',
            'eglise_provenance_epoux' => 'required|string|max:255',
            'eglise_provenance_epouse' => 'required|string|max:255',
            'numero_certificat' => 'required|string|max:255',
            'lieu' => 'required|string|max:255',
            'date' => 'required|date',
        ]);

        $dataMariage->update($champs);
        return $dataMariage;
    }

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