<?php

namespace App\Http\Controllers;

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

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

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

    /**
     * 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'
        ]);

        $dataTypeParent = Type_parente::create($champs);
        return $dataTypeParent;
    }

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

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

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

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