<?php

namespace App\Services;

use App\Repositories\Eloquent\OptionSetRepository;
use App\Repositories\Eloquent\OptionSetItemRepository;

class OptionSetService
{
    public function __construct(
        protected OptionSetRepository $optionSetRepository,
        protected OptionSetItemRepository $optionSetItemRepository,
        protected LoggingService $loggingService
    ) {}

    public function getAll()
    {
        $this->loggingService->logInfo('Fetching all option sets');
        return $this->optionSetRepository->all();
    }

    public function getActive()
    {
        $this->loggingService->logInfo('Fetching active option sets');
        return $this->optionSetRepository->getActive();
    }

    public function getByType(string $type)
    {
        $this->loggingService->logInfo('Fetching option sets by type', ['type' => $type]);
        return $this->optionSetRepository->getByType($type);
    }

    public function find(int $id)
    {
        $this->loggingService->logInfo('Finding option set', ['id' => $id]);
        return $this->optionSetRepository->find($id);
    }

    public function findWithItems(int $id)
    {
        $this->loggingService->logInfo('Finding option set with items', ['id' => $id]);
        return $this->optionSetRepository->getWithActiveItems($id);
    }

    public function create(array $data)
    {
        $this->loggingService->logInfo('Creating option set', ['data' => $data]);

        $items = $data['items'] ?? [];
        unset($data['items']);

        $optionSet = $this->optionSetRepository->create($data);

        foreach ($items as $index => $item) {
            $item['option_set_id'] = $optionSet->id;
            $item['sort_order'] = $item['sort_order'] ?? $index;
            $this->optionSetItemRepository->create($item);
        }

        return $optionSet->fresh('items');
    }

    public function update(int $id, array $data)
    {
        $this->loggingService->logInfo('Updating option set', ['id' => $id, 'data' => $data]);

        unset($data['items']); // Items should be managed separately

        return $this->optionSetRepository->update($id, $data);
    }

    public function delete(int $id)
    {
        $this->loggingService->logInfo('Deleting option set', ['id' => $id]);
        return $this->optionSetRepository->delete($id);
    }

    // Option Set Items
    public function getItems(int $optionSetId)
    {
        $this->loggingService->logInfo('Fetching option set items', ['option_set_id' => $optionSetId]);
        return $this->optionSetItemRepository->getByOptionSet($optionSetId);
    }

    public function createItem(int $optionSetId, array $data)
    {
        $this->loggingService->logInfo('Creating option set item', ['option_set_id' => $optionSetId, 'data' => $data]);

        $data['option_set_id'] = $optionSetId;
        return $this->optionSetItemRepository->create($data);
    }

    public function updateItem(int $itemId, array $data)
    {
        $this->loggingService->logInfo('Updating option set item', ['item_id' => $itemId, 'data' => $data]);
        return $this->optionSetItemRepository->update($itemId, $data);
    }

    public function deleteItem(int $itemId)
    {
        $this->loggingService->logInfo('Deleting option set item', ['item_id' => $itemId]);
        return $this->optionSetItemRepository->delete($itemId);
    }
}
