<?php

namespace App\Domains\Desfile\Controllers;

use App\Domains\Desfile\Requests\ReplaceEntryImageRequest;
use App\Domains\Desfile\Requests\SyncEntryRowsRequest;
use App\Domains\Desfile\Requests\UpdateEntryImageRequest;
use App\Domains\Desfile\Resources\EntryResource;
use App\Domains\Desfile\Services\EntryService;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

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

    public function show(Request $request): EntryResource
    {
        return new EntryResource(
            $this->entryService->current($request->user()->tenant()->firstOrFail()),
        );
    }

    public function update(SyncEntryRowsRequest $request): EntryResource
    {
        return new EntryResource($this->entryService->syncRows(
            $request->user()->tenant()->firstOrFail(),
            $request->validated('rows'),
        ));
    }

    public function replaceImage(ReplaceEntryImageRequest $request): JsonResponse
    {
        return (new EntryResource($this->entryService->replaceImage(
            $request->user()->tenant()->firstOrFail(),
            $request->file('image'),
            $request->boolean('is_enabled', true),
        )))->response();
    }

    public function updateImage(UpdateEntryImageRequest $request): EntryResource
    {
        return new EntryResource($this->entryService->updateImage(
            $request->user()->tenant()->firstOrFail(),
            $request->boolean('is_enabled'),
        ));
    }

    public function destroyImage(Request $request): Response
    {
        $this->entryService->deleteImage($request->user()->tenant()->firstOrFail());

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