<?php

namespace App\Http\Controllers;

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

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

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

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

        $dataTache = Type_activite::create($champs);
        return $dataTache;
    }

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

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

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

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