<?php

namespace App\Filament\Resources\PaymentTransactions\Tables;

use App\Enums\PaymentTransactionStatus;
use App\Enums\PaymentTransactionType;
use Filament\Actions\ViewAction;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;

class PaymentTransactionsTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->defaultSort('created_at', 'desc')
            ->columns([
                TextColumn::make('created_at')
                    ->label(__('admin.payment_transactions.fields.created_at'))
                    ->dateTime()
                    ->sortable(),
                TextColumn::make('status')
                    ->label(__('admin.payment_transactions.fields.status'))
                    ->badge()
                    ->sortable(),
                TextColumn::make('type')
                    ->label(__('admin.payment_transactions.fields.type'))
                    ->badge()
                    ->sortable(),
                TextColumn::make('user.email')
                    ->label(__('admin.payment_transactions.fields.user'))
                    ->searchable()
                    ->placeholder(__('admin.payment_transactions.placeholders.empty')),
                TextColumn::make('creditProduct.name')
                    ->label(__('admin.payment_transactions.fields.credit_product'))
                    ->searchable()
                    ->placeholder(__('admin.payment_transactions.placeholders.empty')),
                TextColumn::make('amount')
                    ->label(__('admin.payment_transactions.fields.amount'))
                    ->money(fn ($record): string => $record->currency ?: 'eur', divideBy: 100)
                    ->sortable()
                    ->placeholder(__('admin.payment_transactions.placeholders.empty')),
                TextColumn::make('credits_expected')
                    ->label(__('admin.payment_transactions.fields.credits_expected'))
                    ->numeric(decimalPlaces: 0)
                    ->sortable()
                    ->placeholder(__('admin.payment_transactions.placeholders.empty')),
                TextColumn::make('credits_granted')
                    ->label(__('admin.payment_transactions.fields.credits_granted'))
                    ->numeric(decimalPlaces: 0)
                    ->sortable(),
                TextColumn::make('stripe_event_type')
                    ->label(__('admin.payment_transactions.fields.stripe_event_type'))
                    ->toggleable(),
                TextColumn::make('stripe_checkout_session_id')
                    ->label(__('admin.payment_transactions.fields.stripe_checkout_session_id_short'))
                    ->copyable()
                    ->searchable()
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('stripe_invoice_id')
                    ->label(__('admin.payment_transactions.fields.stripe_invoice_id_short'))
                    ->copyable()
                    ->searchable()
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('stripe_price_id')
                    ->label(__('admin.payment_transactions.fields.stripe_price_id'))
                    ->copyable()
                    ->searchable()
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('error_message')
                    ->label(__('admin.payment_transactions.fields.error_message'))
                    ->limit(60)
                    ->toggleable(),
                TextColumn::make('invoice_email_sent_at')
                    ->label(__('admin.payment_transactions.fields.invoice_email_sent_at'))
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                SelectFilter::make('status')
                    ->label(__('admin.payment_transactions.fields.status'))
                    ->options(PaymentTransactionStatus::class),
                SelectFilter::make('type')
                    ->label(__('admin.payment_transactions.fields.type'))
                    ->options(PaymentTransactionType::class),
            ])
            ->recordActions([
                ViewAction::make(),
            ]);
    }
}
