<?php

namespace App\Http\Controllers\Api\V1\Product;

use App\Http\Controllers\Controller;
use App\Http\Requests\Api\V1\Product\StoreOptionSetRequest;
use App\Http\Requests\Api\V1\Product\UpdateOptionSetRequest;
use App\Http\Requests\Api\V1\Product\StoreOptionSetItemRequest;
use App\Services\OptionSetService;
use App\Services\ResponseService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class OptionSetController extends Controller
{
    public function __construct(
        protected OptionSetService $optionSetService,
        protected ResponseService $responseService
    ) {}

    public function index(): JsonResponse
    {
        $optionSets = $this->optionSetService->getAll();
        return $this->responseService->success($optionSets, 'Option sets retrieved successfully');
    }

    public function active(): JsonResponse
    {
        $optionSets = $this->optionSetService->getActive();
        return $this->responseService->success($optionSets, 'Active option sets retrieved successfully');
    }

    public function byType(string $type): JsonResponse
    {
        if (!in_array($type, ['scoop', 'size'])) {
            return $this->responseService->error('Invalid type. Must be scoop or size', 422);
        }

        $optionSets = $this->optionSetService->getByType($type);
        return $this->responseService->success($optionSets, 'Option sets by type retrieved successfully');
    }

    public function store(StoreOptionSetRequest $request): JsonResponse
    {
        $optionSet = $this->optionSetService->create($request->validated());
        return $this->responseService->success($optionSet, 'Option set created successfully', 201);
    }

    public function show(int $id): JsonResponse
    {
        $optionSet = $this->optionSetService->findWithItems($id);

        if (!$optionSet) {
            return $this->responseService->error('Option set not found', 404);
        }

        return $this->responseService->success($optionSet, 'Option set retrieved successfully');
    }

    public function update(UpdateOptionSetRequest $request, int $id): JsonResponse
    {
        $optionSet = $this->optionSetService->find($id);

        if (!$optionSet) {
            return $this->responseService->error('Option set not found', 404);
        }

        $optionSet = $this->optionSetService->update($id, $request->validated());
        return $this->responseService->success($optionSet, 'Option set updated successfully');
    }

    public function destroy(int $id): JsonResponse
    {
        $optionSet = $this->optionSetService->find($id);

        if (!$optionSet) {
            return $this->responseService->error('Option set not found', 404);
        }

        $this->optionSetService->delete($id);
        return $this->responseService->success(null, 'Option set deleted successfully');
    }

    // Option Set Items
    public function items(int $optionSetId): JsonResponse
    {
        $items = $this->optionSetService->getItems($optionSetId);
        return $this->responseService->success($items, 'Option set items retrieved successfully');
    }

    public function storeItem(StoreOptionSetItemRequest $request, int $optionSetId): JsonResponse
    {
        $optionSet = $this->optionSetService->find($optionSetId);

        if (!$optionSet) {
            return $this->responseService->error('Option set not found', 404);
        }

        $item = $this->optionSetService->createItem($optionSetId, $request->validated());
        return $this->responseService->success($item, 'Option set item created successfully', 201);
    }

    public function updateItem(Request $request, int $optionSetId, int $itemId): JsonResponse
    {
        $request->validate([
            'name' => 'sometimes|string|max:255',
            'price_modifier' => 'nullable|numeric',
            'is_default' => 'boolean',
            'sort_order' => 'integer|min:0',
            'is_active' => 'boolean',
        ]);

        $item = $this->optionSetService->updateItem($itemId, $request->all());
        return $this->responseService->success($item, 'Option set item updated successfully');
    }

    public function destroyItem(int $optionSetId, int $itemId): JsonResponse
    {
        $this->optionSetService->deleteItem($itemId);
        return $this->responseService->success(null, 'Option set item deleted successfully');
    }
}
