<?php

declare(strict_types=1);


namespace App\Filament\Resources\Posts\Tables;

use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Actions\ViewAction;
use Filament\Forms\Components\Select;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TernaryFilter;
use Filament\Tables\Table;

class PostsTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('title')
                    ->label('标题')
                    ->searchable()
                    ->limit(40)
                    ->description(fn ($record) => $record->category?->name),
                TextColumn::make('content_format')
                    ->label('格式')
                    ->badge()
                    ->formatStateUsing(fn ($state) => $state === 'markdown' ? 'MD' : 'HTML')
                    ->color(fn ($state) => $state === 'markdown' ? 'success' : 'gray'),
                TextColumn::make('status')
                    ->label('状态')
                    ->badge()
                    ->formatStateUsing(fn ($state) => match ($state) {
                        'published' => '已发布',
                        'draft' => '草稿',
                        'private' => '私密',
                        default => $state,
                    })
                    ->color(fn ($state) => match ($state) {
                        'published' => 'success',
                        'draft' => 'warning',
                        default => 'gray',
                    }),
                IconColumn::make('is_sticky')
                    ->label('置顶')
                    ->boolean(),
                TextColumn::make('views')
                    ->label('阅读')
                    ->numeric()
                    ->sortable(),
                TextColumn::make('comment_count')
                    ->label('评论')
                    ->numeric()
                    ->sortable(),
                TextColumn::make('published_at')
                    ->label('发布时间')
                    ->dateTime('Y-m-d H:i')
                    ->sortable(),
            ])
            ->filters([
                SelectFilter::make('status')
                    ->label('状态')
                    ->options([
                        'published' => '已发布',
                        'draft' => '草稿',
                        'private' => '私密',
                    ]),
                SelectFilter::make('content_format')
                    ->label('格式')
                    ->options([
                        'markdown' => 'Markdown',
                        'html' => 'HTML',
                    ]),
                TernaryFilter::make('is_sticky')
                    ->label('置顶'),
            ])
            ->recordActions([
                ViewAction::make(),
                EditAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ])
            ->defaultSort('published_at', 'desc');
    }
}
