<?php

namespace App\Http\Controllers;

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


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

        return [
            'size' => sizeof($dataRespoService),
            'data' => $dataRespoService
        ];
    }
    /**
     * 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',
            'service_id' => 'required|exists:services,id',
            'tache_id' => 'required|exists:taches,id'
        ]);

        $dataRespoService = Responsable_service::create($champs);
        return $dataRespoService;
    }

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

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

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

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