<?php

declare(strict_types=1);

namespace App\Filament\Resources\Articles\Tables;

use App\Domain\Plugin\Hook;
use App\Filament\Support\AdminTable;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TernaryFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;

class ArticlesTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                AdminTable::stickyStart(
                    TextColumn::make('id')
                        ->label(__('admin.fields.id'))
                        ->sortable(),
                ),
                TextColumn::make('category.name')
                    ->label(__('admin.fields.category'))
                    ->searchable(),
                TextColumn::make('user.name')
                    ->label(__('admin.fields.author'))
                    ->searchable(),
                AdminTable::ellipsis(
                    TextColumn::make('title')
                        ->label(__('admin.fields.title'))
                        ->searchable(),
                ),
                AdminTable::ellipsis(
                    TextColumn::make('description')
                        ->label(__('admin.fields.description'))
                        ->searchable()
                        ->toggleable(isToggledHiddenByDefault: true),
                ),
                TextColumn::make('keywords')
                    ->label(__('admin.fields.keywords'))
                    ->searchable()
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('published_at')
                    ->label(__('admin.fields.published_at'))
                    ->dateTime()
                    ->sortable(),
                TextColumn::make('views')
                    ->label(__('admin.fields.views'))
                    ->numeric()
                    ->sortable(),
                TextColumn::make('comments_count')
                    ->label(__('admin.fields.comments_count'))
                    ->numeric()
                    ->sortable(),
                IconColumn::make('stick')
                    ->label(__('admin.fields.stick'))
                    ->boolean(),
                IconColumn::make('visible')
                    ->label(__('admin.fields.visible'))
                    ->boolean(),
                IconColumn::make('close_comment')
                    ->label(__('admin.fields.close_comment'))
                    ->boolean(),
                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),
                TextColumn::make('slug')
                    ->label(__('admin.fields.slug'))
                    ->searchable()
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('content_format')
                    ->label(__('admin.fields.content_format'))
                    ->searchable()
                    ->toggleable(isToggledHiddenByDefault: true),
                ...Hook::collect('filament.article.table.columns'),
            ])
            ->filters([
                SelectFilter::make('category_id')
                    ->label(__('admin.fields.category'))
                    ->relationship('category', 'name')
                    ->searchable()
                    ->preload(),
                TernaryFilter::make('visible')
                    ->label(__('admin.fields.visible')),
                TernaryFilter::make('stick')
                    ->label(__('admin.fields.stick')),
                TernaryFilter::make('close_comment')
                    ->label(__('admin.fields.close_comment')),
                ...Hook::collect('filament.article.table.filters'),
            ])
            ->modifyQueryUsing(function (Builder $query): Builder {
                $modified = Hook::filter('filament.article.table.query', $query);

                return $modified instanceof Builder ? $modified : $query;
            })
            ->recordActions([
                EditAction::make(),
                ...Hook::collect('filament.article.actions'),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }
}
