<?php

namespace App\Domains\FiestaFutbolInfantil\Controllers;

use App\Domains\FiestaFutbolInfantil\Requests\UpsertFoodVariantsRequest;
use App\Domains\FiestaFutbolInfantil\Resources\FoodResource;
use App\Domains\FiestaFutbolInfantil\Services\FoodService;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class FoodController extends Controller
{
    public function __construct(private readonly FoodService $foodService) {}

    public function index(Request $request): FoodResource
    {
        return FoodResource::make(
            $this->foodService->current($request->user()->tenant()->firstOrFail())
        );
    }

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

        return FoodResource::make(
            $this->foodService->upsertMany(
                $tenant,
                $request->validated('variants'),
            )
        );
    }

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

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