<?php

namespace App\Domains\FiestaFutbolInfantil\Controllers;

use App\Domains\FiestaFutbolInfantil\Requests\UpsertEntriesRequest;
use App\Domains\FiestaFutbolInfantil\Resources\EntryResource;
use App\Domains\FiestaFutbolInfantil\Services\EntryService;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use Illuminate\Http\Response;

class EntryController extends Controller
{
    public function __construct(private readonly EntryService $entryService) {}

    public function index(Request $request): AnonymousResourceCollection
    {
        return EntryResource::collection(
            $this->entryService->all($request->user()->tenant()->firstOrFail())
        );
    }

    public function store(UpsertEntriesRequest $request): JsonResponse
    {
        $tenant = $request->user()->tenant()->firstOrFail();

        $entries = $this->entryService->upsertMany(
            $tenant,
            $request->validated('entries'),
        );

        return EntryResource::collection($entries)
            ->response()
            ->setStatusCode(200);
    }

    public function destroy(Request $request, int $entry): Response
    {
        $this->entryService->delete(
            $request->user()->tenant()->firstOrFail(),
            $entry,
        );

        return response()->noContent();
    }
}
