<?php

namespace App\Http\Controllers;

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

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

        return [
            'size' => sizeof($dataRespoChap),
            'data' => $dataRespoChap
        ];
    }
    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $req)
    {
        $champs = $req->validate([
            'debut_mandat' => 'required|date',
            'fin_mandat' => 'required|date',
            'email' => 'required|email',
            'personne_id' => 'required|exists:personnes,id',
            'commission_id' => 'required|exists:commissions,id',
            'tache_id' => 'required|exists:taches,id'
        ]);

        $dataRespoChap = Responsable_commission::create($champs);
        return $dataRespoChap;
    }

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

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $req, $id)
    {
        $dataRespoChap = Responsable_commission::find($id);
        if (!$dataRespoChap) {
            return response()->json(['message' => 'Responsable_commission not found'], 404);
        }
        $champs = $req->validate([
            'debut_mandat' => 'required|date',
            'fin_mandat' => 'required|date',
            'email' => 'required|email',
            'personne_id' => 'required|exists:personnes,id',
            'commission_id' => 'required|exists:commissions,id',
            'tache_id' => 'required|exists:taches,id'
        ]);
        $dataRespoChap->update($champs);
        return $dataRespoChap;
    }

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

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