<?php

namespace App\Http\Controllers;

use App\Models\Type_mariage;
use Illuminate\Http\Request;

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

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

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $req)
    {
        $champs = $req->validate([
            'designation' => 'required|string|max:255',
            'code' => 'required|string|max:255'
        ]);

        $typeEnga = Type_mariage::create($champs);
        return $typeEnga;
    }

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

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $req,  $id)
    {
        $typeEnga = Type_mariage::find($id);
        if (!$typeEnga) {
            return response()->json(['message' => 'Type mariage not found'], 404);
        }
        $champs = $req->validate([
            'designation' => 'required|string|max:255',
            'code' => 'required|string|max:255',
        ]);
        $typeEnga->update($champs);
        return $typeEnga;
    }

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

        return [
            'message' => "Type mariage deleted"
        ];
    }
}