<?php

declare(strict_types=1);

namespace Plugins\Larablog\Payment\Filament\Resources;

use App\Domain\Plugin\PluginManager;
use App\Filament\Support\AdminTable;
use BackedEnum;
use Filament\Actions\ViewAction;
use Filament\Infolists\Components\TextEntry;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Plugins\Larablog\Payment\Filament\Resources\OrderResource\Pages\ListOrders;
use Plugins\Larablog\Payment\Filament\Resources\OrderResource\Pages\ViewOrder;
use Plugins\Larablog\Payment\Models\Order;

class OrderResource extends Resource
{
    protected static ?string $model = Order::class;

    protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedCreditCard;

    protected static ?int $navigationSort = 100;

    public static function getNavigationGroup(): ?string
    {
        return __('admin.groups.plugins');
    }

    public static function getNavigationLabel(): string
    {
        return __('admin.nav.orders');
    }

    public static function getModelLabel(): string
    {
        return __('admin.models.order');
    }

    public static function getPluralModelLabel(): string
    {
        return __('admin.models.orders');
    }

    public static function canAccess(): bool
    {
        return static::pluginEnabled();
    }

    public static function shouldRegisterNavigation(): bool
    {
        return static::pluginEnabled();
    }

    public static function canCreate(): bool
    {
        return false;
    }

    public static function canEdit($record): bool
    {
        return false;
    }

    public static function canDelete($record): bool
    {
        return false;
    }

    protected static function pluginEnabled(): bool
    {
        return app(PluginManager::class)->isEnabled('larablog/payment');
    }

    public static function form(Schema $schema): Schema
    {
        return $schema;
    }

    public static function infolist(Schema $schema): Schema
    {
        return $schema->components([
            TextEntry::make('id')
                ->label(__('admin.fields.id')),
            TextEntry::make('user.name')
                ->label(__('admin.fields.user')),
            TextEntry::make('status')
                ->label(__('admin.fields.status'))
                ->badge(),
            TextEntry::make('amount')
                ->label(__('admin.fields.amount')),
            TextEntry::make('currency')
                ->label(__('admin.fields.currency')),
            TextEntry::make('gateway')
                ->label(__('admin.fields.gateway')),
            TextEntry::make('paid_at')
                ->label(__('admin.fields.paid_at'))
                ->dateTime(),
            TextEntry::make('created_at')
                ->label(__('admin.fields.created_at'))
                ->dateTime(),
        ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                AdminTable::stickyStart(
                    TextColumn::make('id')
                        ->label(__('admin.fields.id'))
                        ->sortable(),
                ),
                TextColumn::make('user.name')
                    ->label(__('admin.fields.user'))
                    ->searchable(),
                TextColumn::make('status')
                    ->label(__('admin.fields.status'))
                    ->badge()
                    ->sortable(),
                TextColumn::make('amount')
                    ->label(__('admin.fields.amount'))
                    ->sortable(),
                TextColumn::make('currency')
                    ->label(__('admin.fields.currency')),
                TextColumn::make('gateway')
                    ->label(__('admin.fields.gateway')),
                TextColumn::make('paid_at')
                    ->label(__('admin.fields.paid_at'))
                    ->dateTime()
                    ->sortable(),
            ])
            ->defaultSort('id', 'desc')
            ->recordActions([
                ViewAction::make(),
            ]);
    }

    public static function getPages(): array
    {
        return [
            'index' => ListOrders::route('/'),
            'view' => ViewOrder::route('/{record}'),
        ];
    }
}
