<?php

namespace App\Filament\Resources\Payments;

use App\Filament\Resources\Payments\Pages\ListPayments;
use App\Models\Payment;
use BackedEnum;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;

class PaymentResource extends Resource
{
    protected static ?string $model = Payment::class;

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

    protected static ?string $navigationLabel = 'Payments';

    protected static string|\UnitEnum|null $navigationGroup = 'Platform';

    protected static ?int $navigationSort = 20;

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

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('user.email')
                    ->label('User')
                    ->searchable()
                    ->sortable(),

                TextColumn::make('razorpay_order_id')
                    ->label('Order ID')
                    ->searchable(),

                TextColumn::make('razorpay_payment_id')
                    ->label('Payment ID')
                    ->searchable(),

                TextColumn::make('amount_rupees')
                    ->label('Amount (Rupees)')
                    ->state(fn (Payment $record): string => $record->formattedAmount()),

                TextColumn::make('currency')
                    ->sortable(),

                TextColumn::make('status')
                    ->badge()
                    ->sortable(),

                TextColumn::make('verified_at')
                    ->dateTime()
                    ->sortable(),

                TextColumn::make('unlocked_at')
                    ->dateTime()
                    ->sortable(),

                TextColumn::make('created_at')
                    ->dateTime()
                    ->sortable(),
            ])
            ->filters([
                SelectFilter::make('status')
                    ->options([
                        Payment::STATUS_CREATED => 'Created',
                        Payment::STATUS_AUTHORIZED => 'Authorized',
                        Payment::STATUS_CAPTURED => 'Captured',
                        Payment::STATUS_FAILED => 'Failed',
                    ]),
            ])
            ->defaultSort('created_at', 'desc');
    }

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