<?php

declare(strict_types=1);

namespace App\Filament\Resources\BankCards\Tables;

use App\Filament\Support\SelectOptionLabels;

use App\Models\BankCard;
use Filament\Actions\ViewAction;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;

class BankCardsTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('id')->label('شناسه')->sortable(),
                TextColumn::make('user.getFilamentName')
                    ->label('کاربر')
                    ->getStateUsing(fn (BankCard $record) => $record->user?->getFilamentName())
                    ->searchable(['user.first_name', 'user.last_name', 'user.cellphone', 'user.national_code'])
                    ->sortable(),
                TextColumn::make('user.cellphone')->label('موبایل')->toggleable(),
                TextColumn::make('bank_name')->label('بانک')->searchable()->toggleable(),
                TextColumn::make('card_number')
                    ->label('شماره کارت')
                    ->formatStateUsing(fn (BankCard $record) => $record->maskedCardNumber() ?? '-')
                    ->searchable(),
                TextColumn::make('shaba')->label('شبا')->searchable()->copyable(),
                TextColumn::make('account_holder_name')->label('به نام')->searchable(),
                IconColumn::make('is_default')->label('پیش‌فرض')->boolean(),
                TextColumn::make('status')
                    ->label('وضعیت')
                    ->badge()
                    ->formatStateUsing(fn (string $state) => $state === BankCard::STATUS_ACTIVE ? 'فعال' : 'غیرفعال')
                    ->color(fn (string $state) => $state === BankCard::STATUS_ACTIVE ? 'success' : 'gray'),
                TextColumn::make('created_at')->label('تاریخ')->jalaliDateTime('Y/m/d H:i')->sortable(),
            ])
            ->filters([
                SelectFilter::make('status')
                    ->label('وضعیت')
                    ->options([
                        BankCard::STATUS_ACTIVE => 'فعال',
                        BankCard::STATUS_INACTIVE => 'غیرفعال',
                    ]),
                SelectFilter::make('user_id')
                    ->label('کاربر')
                    ->relationship('user', 'first_name')
                    ->searchable(['first_name', 'last_name', 'cellphone', 'national_code', 'email'])
                    ->getOptionLabelFromRecordUsing(fn ($record) => SelectOptionLabels::user($record)),
                SelectFilter::make('is_default')
                    ->label('پیش‌فرض')
                    ->options([
                        '1' => 'بله',
                        '0' => 'خیر',
                    ])
                    ->query(fn (Builder $q, array $data) => filled($data['value'] ?? null)
                        ? $q->where('is_default', (bool) $data['value'])
                        : $q),
            ])
            ->recordActions([
                ViewAction::make()
                    ->slideOver()
                    ->modalWidth('xl')
                    ->fillForm(fn (BankCard $record): array => [
                        'user' => $record->user?->getFilamentName(),
                        'bank_name' => $record->bank_name ?? '-',
                        'card_number' => $record->maskedCardNumber() ?? '-',
                        'shaba' => $record->shaba,
                        'account_holder_name' => $record->account_holder_name,
                        'is_default' => $record->is_default,
                        'status' => $record->status === BankCard::STATUS_ACTIVE ? 'فعال' : 'غیرفعال',
                    ])
                    ->form([
                        TextInput::make('user')->label('کاربر')->disabled(),
                        TextInput::make('bank_name')->label('بانک')->disabled(),
                        TextInput::make('card_number')->label('شماره کارت')->disabled(),
                        TextInput::make('shaba')->label('شبا')->disabled(),
                        TextInput::make('account_holder_name')->label('به نام')->disabled(),
                        Toggle::make('is_default')->label('پیش‌فرض')->disabled(),
                        TextInput::make('status')->label('وضعیت')->disabled(),
                    ]),
            ])
            ->defaultSort('created_at', 'desc');
    }
}
