<?php
namespace App\Http\Controllers\Client;

use App\Http\Controllers\Controller;
use App\Http\Requests\Client\StoreSignalementRequest;
use App\Http\Resources\Client\SignalementResource;
use App\Services\SignalementService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class SignalementController extends Controller
{
    public function __construct(private readonly SignalementService $signalementService) {}

    public function index(Request $request): JsonResponse
    {
        $client = $request->user()->client()->firstOrFail();
        $statut = $request->query('statut');
        
        $signalements = $this->signalementService->listerSignalementsClient($client, $statut);

        return response()->json([
            'signalements' => SignalementResource::collection($signalements),
            'pagination' => [
                'current_page' => $signalements->currentPage(),
                'last_page' => $signalements->lastPage(),
                'per_page' => $signalements->perPage(),
                'total' => $signalements->total(),
            ],
        ]);
    }

    public function store(StoreSignalementRequest $request): JsonResponse
    {
        $client = $request->user()->client()->firstOrFail();
        $signalement = $this->signalementService->creerSignalement($client, $request->validated());

        return response()->json([
            'message' => 'Votre signalement a bien été enregistré.',
            'signalement' => new SignalementResource($signalement),
        ], 201);
    }

    public function show(Request $request, int $id): JsonResponse
    {
        $client = $request->user()->client()->firstOrFail();
        $signalement = $this->signalementService->getSignalementDuClient($client, $id);

        return response()->json([
            'signalement' => new SignalementResource($signalement),
        ]);
    }
}
