<?php

declare(strict_types=1);

namespace App\Filament\Resources\Comments\Tables;

use App\Domain\Blog\ArticleExcerpt;
use App\Filament\Support\AdminTable;
use App\Models\Comment;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;

class CommentsTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->modifyQueryUsing(fn ($query) => $query->with('article'))
            ->columns([
                AdminTable::ellipsis(
                    TextColumn::make('article.title')
                        ->label(__('admin.fields.article'))
                        ->searchable(),
                ),
                AdminTable::ellipsis(
                    TextColumn::make('article_excerpt')
                        ->label(__('admin.fields.article_excerpt'))
                        ->getStateUsing(function (Comment $record): string {
                            if ($record->article === null) {
                                return '';
                            }

                            return app(ArticleExcerpt::class)->forList($record->article, 80);
                        }),
                ),
                TextColumn::make('author')
                    ->label(__('admin.fields.author'))
                    ->searchable(),
                AdminTable::ellipsis(
                    TextColumn::make('url')
                        ->label(__('admin.fields.url'))
                        ->searchable(),
                ),
                TextColumn::make('ip')
                    ->label(__('admin.fields.ip'))
                    ->searchable(),
                TextColumn::make('moderation_status')
                    ->label(__('admin.fields.moderation_status'))
                    ->searchable(),
                TextColumn::make('published_at')
                    ->label(__('admin.fields.published_at'))
                    ->dateTime()
                    ->sortable(),
                TextColumn::make('created_at')
                    ->label(__('admin.fields.created_at'))
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('updated_at')
                    ->label(__('admin.fields.updated_at'))
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                SelectFilter::make('moderation_status')
                    ->label(__('admin.fields.moderation_status'))
                    ->options([
                        Comment::STATUS_PENDING => __('admin.options.moderation.pending'),
                        Comment::STATUS_PENDING_AI => __('admin.options.moderation.pending_ai'),
                        Comment::STATUS_APPROVED => __('admin.options.moderation.approved'),
                        Comment::STATUS_REJECTED => __('admin.options.moderation.rejected'),
                        Comment::STATUS_NEEDS_HUMAN => __('admin.options.moderation.needs_human'),
                    ]),
            ])
            ->recordActions([
                EditAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }
}
