<?php

namespace Modules\Finance;

use App\Services\Module\AbstractModuleServiceProvider;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Route;
use Modules\Finance\Clients\FinanceModuleClient;

class FinanceServiceProvider extends AbstractModuleServiceProvider
{
    public function register(): void
    {
        $this->app->singleton(FinanceModuleClient::class);
    }

    public function bootModule(): void
    {
        Route::bind('transaction', function (string $value) {
            return $this->app->make(FinanceModuleClient::class)->findTransaction((int) $value);
        });

        Route::bind('category', function (string $value) {
            $client = $this->app->make(FinanceModuleClient::class);
            $routeName = request()->route()?->getName() ?? '';

            if (str_contains($routeName, 'income-categories')) {
                return $client->findIncomeCategory((int) $value);
            }

            return $client->findExpenseCategory((int) $value);
        });

        parent::bootModule();

        Route::getRoutes()->refreshNameLookups();
    }

    protected function modulePath(): string
    {
        return __DIR__;
    }

    protected function moduleSlug(): string
    {
        return 'finance';
    }

    protected function moduleName(): string
    {
        return 'Finance';
    }

    protected function moduleVersion(): string
    {
        return '1.0.0';
    }

    protected function modulePermissions(): array
    {
        return (array) config('permissions.modules.Finance', []);
    }

    protected function moduleNavigation(): array
    {
        return [
            [
                'route' => 'finance.dashboard',
                'icon' => 'bi-speedometer2',
                'label' => 'Dashboard',
                'permission' => 'FINANCE_DASHBOARD_VIEW',
            ],
            [
                'route' => 'finance.transactions.index',
                'icon' => 'bi-receipt',
                'label' => 'Transactions',
                'permission' => 'FINANCE_TRANSACTIONS_VIEW',
            ],
            [
                'route' => 'finance.income-categories.index',
                'icon' => 'bi-arrow-down-circle',
                'label' => 'Income Categories',
                'permission' => 'FINANCE_CATEGORIES_MANAGE',
            ],
            [
                'route' => 'finance.expense-categories.index',
                'icon' => 'bi-arrow-up-circle',
                'label' => 'Expense Categories',
                'permission' => 'FINANCE_CATEGORIES_MANAGE',
            ],
            [
                'route' => 'finance.reports.index',
                'icon' => 'bi-bar-chart',
                'label' => 'Reports',
                'permission' => 'FINANCE_REPORTS_VIEW',
            ],
        ];
    }

    protected function bootPolicies(): void
    {
        $gates = [
            'finance.dashboard.view' => 'FINANCE_DASHBOARD_VIEW',
            'finance.transactions.view' => 'FINANCE_TRANSACTIONS_VIEW',
            'finance.transactions.create' => 'FINANCE_TRANSACTIONS_CREATE',
            'finance.transactions.update' => 'FINANCE_TRANSACTIONS_UPDATE',
            'finance.transactions.delete' => 'FINANCE_TRANSACTIONS_DELETE',
            'finance.categories.manage' => 'FINANCE_CATEGORIES_MANAGE',
            'finance.reports.view' => 'FINANCE_REPORTS_VIEW',
            'finance.export' => 'FINANCE_EXPORT',
        ];

        foreach ($gates as $ability => $permission) {
            Gate::define($ability, fn ($user) => $user->hasPermissionTo($permission));
        }
    }
}
