<?php

namespace App\Services;

use App\Repositories\Eloquent\InnerOptionSetRepository;
use App\Repositories\Eloquent\InnerOptionItemRepository;

class InnerOptionSetService
{
    public function __construct(
        protected InnerOptionSetRepository $innerOptionSetRepository,
        protected InnerOptionItemRepository $innerOptionItemRepository,
        protected LoggingService $loggingService
    ) {}

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

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

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

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

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

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

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

        $innerOptionSet = $this->innerOptionSetRepository->create($data);

        foreach ($items as $index => $item) {
            $item['inner_option_set_id'] = $innerOptionSet->id;
            $item['sort_order'] = $item['sort_order'] ?? $index;
            $this->innerOptionItemRepository->create($item);
        }

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

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

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

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

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

    // Inner Option Items
    public function getItems(int $innerOptionSetId)
    {
        $this->loggingService->logInfo('Fetching inner option items', ['inner_option_set_id' => $innerOptionSetId]);
        return $this->innerOptionItemRepository->getByInnerOptionSet($innerOptionSetId);
    }

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

        $data['inner_option_set_id'] = $innerOptionSetId;
        return $this->innerOptionItemRepository->create($data);
    }

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

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