<?php

namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Http\Requests\StoreRapportRequest;
use App\Http\Resources\RapportResource;
use App\Models\Rapport;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class RapportController extends Controller
{
    public function index()
    {
        Log::info('RapportController: index called', [
            'user_id' => auth()->id(),
            'user_email' => auth()->user()?->email,
        ]);

        $rapports = Rapport::all();
        return RapportResource::collection($rapports);
    }


    public function store(StoreRapportRequest $request)
    {
        Log::info('RapportController: store called', [
            'user_id' => auth()->id(),
            'user_email' => auth()->user()?->email,
            'request_data' => $request->all(),
        ]);

        $rapport = Rapport::create($request->validated());

        Log::info('RapportController: rapport created successfully', [
            'rapport_id' => $rapport->id,
            'user_id' => auth()->id(),
        ]);

        return new RapportResource($rapport);
    }
    
    public function show(Rapport $rapport)
    {
        return new RapportResource($rapport);
    }

    public function update(StoreRapportRequest $request, Rapport $rapport)
    {
        $rapport->update($request->validated());
        return new RapportResource($rapport);
    }

    public function destroy(Rapport $rapport)
    {
        $rapport->delete();
        return response()->json(['message' => 'Rapport supprimé'], 200);
    }
}
