<?php

namespace App\Filament\Resources\DelayedGoods\Tables;

use App\Filament\Actions\Cheerful\DeleteAction;
use App\Filament\Actions\Cheerful\EditAction;
use App\Filament\Actions\Cheerful\ForceDeleteAction;
use App\Filament\Actions\Cheerful\RestoreAction;
use App\Filament\Actions\DefaultBulkActions;
use App\Filament\Columns\TimestampColumns;
use Filament\Actions\BulkActionGroup;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\TrashedFilter;
use Filament\Tables\Table;

class DelayedGoodsTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('stored_date')
                    ->label('Tanggal Penyimpanan')
                    ->date('l, d F Y')
                    ->sortable(),

                TextColumn::make('customer.name')
                    ->label('Pelanggan')
                    ->searchable()
                    ->sortable(),

                TextColumn::make('quantity')
                    ->label('Jumlah')
                    ->suffix(fn ($record) => ' '.$record->unit?->alias)
                    ->numeric()
                    ->sortable(),

                TextColumn::make('unit_price')
                    ->label('Harga per Satuan')
                    ->money('IDR', decimalPlaces: 0)
                    ->sortable(),

                TextColumn::make('total_amount')
                    ->label('Total')
                    ->money('IDR', decimalPlaces: 0)
                    ->sortable(),

                TextColumn::make('paid_amount')
                    ->label('Sudah Dibayar')
                    ->money('IDR', decimalPlaces: 0)
                    ->sortable()
                    ->color(fn ($record) => $record->paid_amount >= $record->total_amount ? 'success' : 'warning'),

                TextColumn::make('remaining_amount')
                    ->label('Sisa')
                    ->getStateUsing(fn ($record) => max(0, $record->total_amount - $record->paid_amount))
                    ->money('IDR', decimalPlaces: 0)
                    ->sortable()
                    ->color(fn ($record) => $record->paid_amount >= $record->total_amount ? 'success' : 'danger'),

                IconColumn::make('is_paid')
                    ->label('Status')
                    ->boolean()
                    ->trueIcon(Heroicon::CheckCircle)
                    ->falseIcon(Heroicon::XCircle)
                    ->trueColor('success')
                    ->falseColor('warning')
                    ->sortable(),

                TextColumn::make('notes')
                    ->label('Catatan')
                    ->limit(30)
                    ->toggleable(isToggledHiddenByDefault: true),

                TextColumn::make('payment_date')
                    ->label('Tanggal Lunas')
                    ->date('l, d F Y')
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),

                ...TimestampColumns::make(),
            ])
            ->filters([
                TrashedFilter::make()
                    ->native(false),
            ])
            ->recordActions([
                EditAction::make()
                    ->label('Ubah'),

                DeleteAction::make(),

                ForceDeleteAction::make(),

                RestoreAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    ...DefaultBulkActions::make('Barang Tertunda'),
                ]),
            ])
            ->emptyStateIcon(Heroicon::Clock)
            ->emptyStateDescription('Setelah Anda membuat data pertama, maka akan muncul disini.')
            ->defaultSort('created_at', 'desc')
            ->deferFilters(false);
    }
}
