<?php

namespace App\ServiceProviders;

use App\Exceptions\CustomExceptionHandler;
use App\Filters\ProductAttribute;
use App\Filters\ProductCategories;
use App\Filters\ProductCategory;
use App\Filters\ProductManufacturer;
use App\Filters\ProductModel;
use App\Filters\ProductPrice;
use App\Filters\ProductQuantity;
use App\Filters\ProductStatus;
use App\Telegram\LinkCommand;
use Openguru\OpenCartFramework\Container\ServiceProvider;
use Openguru\OpenCartFramework\Contracts\ExceptionHandlerInterface;
use Openguru\OpenCartFramework\CriteriaBuilder\RulesRegistry;
use Openguru\OpenCartFramework\Telegram\Commands\ChatIdCommand;
use Openguru\OpenCartFramework\Telegram\Commands\StartCommand;
use Openguru\OpenCartFramework\Telegram\TelegramCommandsRegistry;

class AppServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->container->singleton(ExceptionHandlerInterface::class, function () {
            return new CustomExceptionHandler();
        });

        $this->registerTelegramCommands();
        $this->registerFacetFilters();
    }

    private function registerTelegramCommands(): void
    {
        $this->container->singleton(TelegramCommandsRegistry::class, function () {
            return new TelegramCommandsRegistry();
        });

        $registry = $this->container->get(TelegramCommandsRegistry::class);

        $registry->addCommand('id', ChatIdCommand::class, 'Возвращает ChatID текущего чата.');
        $registry->addCommand('link', LinkCommand::class, 'Генератор Telegram сообщений с кнопкой');
        $registry->addCommand(
            'start',
            StartCommand::class,
            'Базовая команда Telegram бота. Присылает ссылку на открытие Telecart магазина.'
        );
    }

    private function registerFacetFilters(): void
    {
        $this->container->singleton(RulesRegistry::class, function () {
            return new RulesRegistry();
        });

        $registry = $this->container->get(RulesRegistry::class);
        $registry->register([
            ProductAttribute::NAME => ProductAttribute::class,
            ProductCategories::NAME => ProductCategories::class,
            ProductManufacturer::NAME => ProductManufacturer::class,
            ProductModel::NAME => ProductModel::class,
            ProductPrice::NAME => ProductPrice::class,
            ProductQuantity::NAME => ProductQuantity::class,
            ProductStatus::NAME => ProductStatus::class,
            ProductCategory::NAME => ProductCategory::class,
        ]);
    }
}
