<?php

declare(strict_types=1);

namespace App\Filament\Resources\Posts\Tables;

use App\Enums\Status;
use App\Filament\Support\SelectOptionLabels;
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\ImageColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TrashedFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;

class PostsTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->modifyQueryUsing(fn (Builder $query): Builder => $query->with(['author', 'categories']))
            ->defaultSort('created_at', 'desc')
            ->columns([
                ImageColumn::make('image')
                    ->label('تصویر')
                    ->disk('public')
                    ->circular()
                    ->width(40)
                    ->defaultImageUrl(url('files/default-post.png')),
                TextColumn::make('title')
                    ->label('عنوان')
                    ->searchable()
                    ->sortable()
                    ->limit(40)
                    ->wrap(),
                TextColumn::make('slug')
                    ->label('نامک')
                    ->searchable()
                    ->toggleable()
                    ->toggledHiddenByDefault(),
                TextColumn::make('author.name')
                    ->label('نویسنده')
                    ->searchable()
                    ->sortable(),
                TextColumn::make('views')
                    ->label('بازدید')
                    ->numeric()
                    ->sortable(),
                TextColumn::make('time_to_read')
                    ->label('زمان مطالعه (دقیقه)')
                    ->sortable()
                    ->toggleable(),
                IconColumn::make('is_featured')
                    ->label('ویژه')
                    ->boolean(),
                TextColumn::make('status')
                    ->label('وضعیت')
                    ->badge()
                    ->formatStateUsing(fn ($state) => Status::tryFrom((int) $state)?->label() ?? (string) $state)
                    ->color(fn ($state) => Status::tryFrom((int) $state)?->color() ?? 'gray')
                    ->sortable(),
                TextColumn::make('publish_at')
                    ->label('تاریخ انتشار')
                    ->jalaliDateTime('Y/m/d H:i')
                    ->sortable()
                    ->placeholder('-'),
                TextColumn::make('created_at')
                    ->label('تاریخ ایجاد')
                    ->jalaliDateTime('Y/m/d H:i')
                    ->sortable()
                    ->toggleable(),
            ])
            ->filters([
                TrashedFilter::make(),
                SelectFilter::make('status')
                    ->label('وضعیت')
                    ->options(collect(Status::get())->mapWithKeys(fn ($state) => [$state['value'] => $state['label']])->all()),
                SelectFilter::make('is_featured')
                    ->label('ویژه')
                    ->options([
                        1 => 'بله',
                        0 => 'خیر',
                    ]),
                SelectFilter::make('author_id')
                    ->label('نویسنده')
                    ->relationship('author', 'first_name')
                    ->getOptionLabelFromRecordUsing(fn ($record) => SelectOptionLabels::user($record))
                    ->searchable(['first_name', 'last_name', 'cellphone', 'email'])
                    ->preload(),
            ])
            ->recordActions([
                EditAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                    ForceDeleteBulkAction::make(),
                    RestoreBulkAction::make(),
                ]),
            ]);
    }
}
