<?php

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

use App\Http\Controllers\Controller;
use App\Http\Requests\Api\V1\Product\StoreInnerOptionSetRequest;
use App\Http\Requests\Api\V1\Product\UpdateInnerOptionSetRequest;
use App\Http\Requests\Api\V1\Product\StoreInnerOptionItemRequest;
use App\Services\InnerOptionSetService;
use App\Services\ResponseService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class InnerOptionSetController extends Controller
{
    public function __construct(
        protected InnerOptionSetService $innerOptionSetService,
        protected ResponseService $responseService
    ) {}

    public function index(): JsonResponse
    {
        $innerOptionSets = $this->innerOptionSetService->getAll();
        return $this->responseService->success($innerOptionSets, 'Inner option sets retrieved successfully');
    }

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

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

        $innerOptionSets = $this->innerOptionSetService->getByType($type);
        return $this->responseService->success($innerOptionSets, 'Inner option sets by type retrieved successfully');
    }

    public function store(StoreInnerOptionSetRequest $request): JsonResponse
    {
        $innerOptionSet = $this->innerOptionSetService->create($request->validated());
        return $this->responseService->success($innerOptionSet, 'Inner option set created successfully', 201);
    }

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

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

        return $this->responseService->success($innerOptionSet, 'Inner option set retrieved successfully');
    }

    public function update(UpdateInnerOptionSetRequest $request, int $id): JsonResponse
    {
        $innerOptionSet = $this->innerOptionSetService->find($id);

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

        $innerOptionSet = $this->innerOptionSetService->update($id, $request->validated());
        return $this->responseService->success($innerOptionSet, 'Inner option set updated successfully');
    }

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

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

        $this->innerOptionSetService->delete($id);
        return $this->responseService->success(null, 'Inner option set deleted successfully');
    }

    // Inner Option Items
    public function items(int $innerOptionSetId): JsonResponse
    {
        $items = $this->innerOptionSetService->getItems($innerOptionSetId);
        return $this->responseService->success($items, 'Inner option items retrieved successfully');
    }

    public function storeItem(StoreInnerOptionItemRequest $request, int $innerOptionSetId): JsonResponse
    {
        $innerOptionSet = $this->innerOptionSetService->find($innerOptionSetId);

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

        $item = $this->innerOptionSetService->createItem($innerOptionSetId, $request->validated());
        return $this->responseService->success($item, 'Inner option item created successfully', 201);
    }

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

        $data = $request->all();

        // Map price_modifier to price if provided
        if (isset($data['price_modifier']) && !isset($data['price'])) {
            $data['price'] = $data['price_modifier'];
        }
        unset($data['price_modifier']);

        $item = $this->innerOptionSetService->updateItem($itemId, $data);
        return $this->responseService->success($item, 'Inner option item updated successfully');
    }

    public function destroyItem(int $innerOptionSetId, int $itemId): JsonResponse
    {
        $this->innerOptionSetService->deleteItem($itemId);
        return $this->responseService->success(null, 'Inner option item deleted successfully');
    }
}
