<?php

namespace App\Filament\Clusters\Loans\Resources\LoanPaidOffLetters\Tables;

use App\Modules\DefaultQueryForResourceIndex\Repositories\DefaultQueryForResourceIndexRepository;
use App\Modules\OrderStatus\Repositories\OrderStatusRepository;
use App\Modules\Region\Repositories\RegionRepository;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;

class LoanPaidOffLettersTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->modifyQueryUsing(function (Builder $query) {
                DefaultQueryForResourceIndexRepository::make($query);
            })
            ->columns([
                TextColumn::make('id')
                    ->label('ID')
                    ->sortable(),

                TextColumn::make('region')
                    ->label(__('Region'))
                    ->formatStateUsing(fn (string $state) => RegionRepository::label($state)),

                TextColumn::make('branch.name')
                    ->label(__('Branch')),

                TextColumn::make('customer_name')
                    ->label(__('Name'))
                    ->searchable(),

                TextColumn::make('customer_surname')
                    ->label(__('Surname'))
                    ->searchable(),

                TextColumn::make('phone')
                    ->label(__('Phone'))
                    ->searchable(),

                TextColumn::make('status')
                    ->label(__('Status'))
                    ->formatStateUsing(fn (string $state) => OrderStatusRepository::statusFormatted($state))
                    ->sortable()
                    ->badge()
                    ->searchable(),

                TextColumn::make('created_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('updated_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                //
            ])
            ->recordActions([
                EditAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }
}
