<?php

namespace App\Domains\FiestaFutbolInfantil\Controllers;

use App\Domains\FiestaFutbolInfantil\Enums\FiestaCategory;
use App\Domains\FiestaFutbolInfantil\Requests\UpdateCategoryVisibilityRequest;
use App\Domains\FiestaFutbolInfantil\Services\CategoryVisibilityService;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CategoryVisibilityController extends Controller
{
    public function __construct(private readonly CategoryVisibilityService $visibilityService) {}

    public function show(Request $request): JsonResponse
    {
        $category = $this->category($request);
        $tenant = $request->user()->tenant()->firstOrFail();

        return response()->json([
            'data' => [
                'is_enabled' => $this->visibilityService->isEnabled($tenant, $category),
            ],
        ]);
    }

    public function update(UpdateCategoryVisibilityRequest $request): JsonResponse
    {
        $category = $this->category($request);
        $tenant = $request->user()->tenant()->firstOrFail();
        $model = $this->visibilityService->update(
            $tenant,
            $category,
            $request->boolean('is_enabled'),
        );

        return response()->json([
            'data' => [
                'is_enabled' => $model->is_enabled,
            ],
            'message' => $model->is_enabled
                ? 'Mostrar en sitio web se activo correctamente'
                : 'Mostrar en sitio web se desactivo correctamente',
        ]);
    }

    private function category(Request $request): FiestaCategory
    {
        return FiestaCategory::from((string) $request->route('category'));
    }
}
