<?php

declare(strict_types=1);

namespace App\Filament\Resources\MarketPrices\Tables;

use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteAction;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;

class MarketPricesTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->modifyQueryUsing(fn ($query) => $query->with(['category', 'breed', 'province', 'county']))
            ->defaultSort('recorded_at', 'desc')
            ->columns([
                TextColumn::make('label')->label('عنوان')->searchable()->sortable(),
                TextColumn::make('price')
                    ->label('قیمت')
                    ->formatStateUsing(fn ($state) => blank($state) ? '-' : number_format((int) $state) . ' تومان')
                    ->sortable(),
                TextColumn::make('change')
                    ->label('تغییر %')
                    ->badge()
                    ->color(fn ($state) => (float) $state >= 0 ? 'success' : 'danger')
                    ->sortable(),
                TextColumn::make('category.title')->label('دسته')->placeholder('-'),
                TextColumn::make('breed.name')->label('نژاد')->placeholder('-'),
                TextColumn::make('province.name')->label('استان')->placeholder('-'),
                TextColumn::make('county.name')->label('شهر')->placeholder('-'),
                TextColumn::make('recorded_at')->label('زمان')->jalaliDateTime('Y/m/d H:i')->sortable(),
            ])
            ->filters([
                SelectFilter::make('category_id')->label('دسته')->relationship('category', 'title')->searchable()->preload(),
                SelectFilter::make('breed_id')->label('نژاد')->relationship('breed', 'name')->searchable()->preload(),
                SelectFilter::make('province_id')->label('استان')->relationship('province', 'name')->searchable()->preload(),
            ])
            ->recordActions([
                EditAction::make(),
                DeleteAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }
}
