<?php

namespace App\Filament\Resources\Payments\Tables;

use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Actions\ForceDeleteAction;
use Filament\Actions\RestoreAction;
use Filament\Tables;
use Filament\Tables\Filters\TrashedFilter;
use Filament\Tables\Table;

class PaymentsTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('contract.contract_number')
                    ->label('Hợp đồng')
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('amount')
                    ->label('Số tiền')
                    ->money('VND')
                    ->sortable(),
                Tables\Columns\TextColumn::make('paid_date')
                    ->label('Ngày thu')
                    ->date('d/m/Y')
                    ->sortable(),
                Tables\Columns\TextColumn::make('method')
                    ->label('Phương thức')
                    ->badge(),
                Tables\Columns\TextColumn::make('receipt_number')
                    ->label('Số phiếu thu')
                    ->searchable(),
                Tables\Columns\TextColumn::make('scheduleItem.type')
                    ->label('Loại đợt')
                    ->placeholder('Tạm ứng')
                    ->formatStateUsing(fn ($state) => $state?->getLabel()),

                Tables\Columns\TextColumn::make('scheduleItem.installment_no')
                    ->label('Đợt TT')
                    ->placeholder('Tạm ứng'),

                Tables\Columns\TextColumn::make('reconciliation_status')
                    ->label('Đối soát')
                    ->badge()
                    ->color(function ($record) {
                        if (! $record->scheduleItem) {
                            return 'gray';
                        }
                        $remaining = (float) $record->scheduleItem->remaining_amount;
                        if ($remaining == 0) {
                            return 'success';
                        }
                        if ($remaining > 0) {
                            return 'warning';
                        }
                        return 'danger';
                    })
                    ->state(function ($record) {
                        if (! $record->scheduleItem) {
                            return 'Tạm ứng';
                        }
                        $remaining = (float) $record->scheduleItem->remaining_amount;
                        if ($remaining == 0) {
                            return 'Đủ';
                        }
                        if ($remaining > 0) {
                            return 'Thiếu';
                        }
                        return 'Thừa';
                    }),

                Tables\Columns\TextColumn::make('scheduleItem.remaining_amount')
                    ->label('Còn thiếu')
                    ->money('VND')
                    ->placeholder('-')
                    ->color('danger'),

                Tables\Columns\TextColumn::make('collector.name')
                    ->label('Ngườ thu')
                    ->placeholder('-')
                    ->sortable(),
            ])
            ->filters([
                Tables\Filters\SelectFilter::make('method')
                    ->label('Phương thức')
                    ->options([
                        'Chuyển khoản' => 'Chuyển khoản',
                        'Tiền mặt' => 'Tiền mặt',
                        'Thẻ' => 'Thẻ',
                        'Khác' => 'Khác',
                    ]),
                Tables\Filters\Filter::make('paid_date')
                    ->label('Ngày thu')
                    ->form([
                        \Filament\Forms\Components\DatePicker::make('from')->label('Từ ngày'),
                        \Filament\Forms\Components\DatePicker::make('to')->label('Đến ngày'),
                    ])
                    ->query(function ($query, array $data) {
                        return $query
                            ->when($data['from'], fn ($q) => $q->whereDate('paid_date', '>=', $data['from']))
                            ->when($data['to'], fn ($q) => $q->whereDate('paid_date', '<=', $data['to']));
                    }),
                TrashedFilter::make(),
            ])
            ->recordActions([
                EditAction::make(),
                RestoreAction::make(),
                ForceDeleteAction::make(),
            ])
            ->bulkActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ])
            ->defaultSort('paid_date', 'desc');
    }
}
