<?php

namespace App\Http\Controllers;

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



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

        return [
            'size' => sizeof($dataRapport),
            'data' => $dataRapport
        ];
    }
    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $req)
    {
        $champs = $req->validate([
            'date' => 'required|date',
            'sujet' => 'required|string|max:255',
            'description' => 'required|string|max:255',
            'homme' => 'required|numeric',
            'femme' => 'required|numeric',
            'enfant' => 'required|numeric',
            'invite' => 'required|numeric',
            'offrande' => 'required|numeric',
            'intervenant' => 'required|string|string',
            'moderateur' => 'required|string|string',
            'chantre' => 'required|string|string',
            'type_activite_id' => 'required|exists:type_activites,id',
            'responsable_service_id' => 'required|exists:responsable_services,id'
        ]);

        $dataRapport = Rapport_activite::create($champs);
        return $dataRapport;
    }

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

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $req, $id)
    {
        $dataRapport = Rapport_activite::find($id);
        if (!$dataRapport) {
            return response()->json(['message' => 'Rapport_activite not found'], 404);
        }
        $champs = $req->validate([
            'date' => 'required|date',
            'sujet' => 'required|string|max:255',
            'description' => 'required|string|max:255',
            'homme' => 'required|numeric',
            'femme' => 'required|numeric',
            'enfant' => 'required|numeric',
            'invite' => 'required|numeric',
            'offrande' => 'required|numeric',
            'Intervenant' => 'required|string|string',
            'moderateur' => 'required|string|string',
            'chantre' => 'required|string|string',
            'type_activite_id' => 'required|exists:type-activites,id',
            'responsable_service_id' => 'required|exists:responsable_services,id'
        ]);
        $dataRapport->update($champs);
        return $dataRapport;
    }

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

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