<?php

namespace App\Filament\Clusters\Cards\CardOrders\Tables;

use App\Modules\CardOrder\Filament\Actions\PayCardOrderAction;
use App\Modules\CardOrder\Models\CardOrder;
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\Actions\ForceDeleteBulkAction;
use Filament\Actions\RestoreBulkAction;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\TrashedFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;

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

                TextColumn::make('cardState.name')
                    ->label(__('Reason'))
                    ->searchable(),

                TextColumn::make('cardType.name')
                    ->label(__('Card type'))
                    ->searchable(),

                TextColumn::make('created_at')
                    ->label(__('Created At'))
                    ->dateTime()
                    ->sortable(),

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

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

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

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

                IconColumn::make('paid')
                    ->label(__('Paid'))
                    ->boolean(),

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

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

                TextColumn::make('updated_at')
                    ->label(__('Updated At'))
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('deleted_at')
                    ->label(__('Deleted At'))
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                TrashedFilter::make(),
            ])
            ->recordActions([
                PayCardOrderAction::make()
                    ->hidden(fn (CardOrder $record) => $record->paid),

                EditAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                    ForceDeleteBulkAction::make(),
                    RestoreBulkAction::make(),
                ]),
            ]);
    }
}
