<?php

namespace App\Http\Controllers;

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

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

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

        $catAdmin = Categorie_admin::create($champs);
        return $catAdmin;
    }

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

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

        $champs = $req->validate([
            'designation' => 'required|string|max:255',
            'code' => 'required|string|max:255',
        ]);
        $catAdmin->update($champs);
        return $catAdmin;
    }

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

        return [
            'message' => "Categorie Admin deleted"
        ];
    }
}