<?php

declare(strict_types=1);

namespace Maestro\Core\Providers;

use Illuminate\Support\ServiceProvider;
use Maestro\Core\DataTables\DataTableColumnCatalog;
use Maestro\Core\DataTables\DataTableColumnDefaultsMerger;
use Maestro\Core\DataTables\DataTableQueryApplier;
use Maestro\Core\DataTables\DataTableRegistry;
use Maestro\Core\DataTables\Resources\ActivityLogDataTable;
use Maestro\Core\DataTables\Resources\RolesDataTable;
use Maestro\Core\DataTables\Resources\UsersDataTable;

final class DataTablesServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->app->singleton(DataTableQueryApplier::class, fn (): DataTableQueryApplier => new DataTableQueryApplier);
        $this->app->singleton(DataTableColumnDefaultsMerger::class, fn (): DataTableColumnDefaultsMerger => new DataTableColumnDefaultsMerger);

        $this->app->singleton(DataTableColumnCatalog::class, function ($app): DataTableColumnCatalog {
            return new DataTableColumnCatalog($app->make(DataTableRegistry::class));
        });

        $this->app->singleton(DataTableRegistry::class, function (): DataTableRegistry {
            $registry = new DataTableRegistry;
            $registry->register(UsersDataTable::class, 'users');
            $registry->register(RolesDataTable::class, 'roles');
            $registry->register(ActivityLogDataTable::class, 'activity-log');

            return $registry;
        });
    }
}
