<?php

declare(strict_types=1);

namespace App\Filament\Support;

use Filament\Tables\Columns\Column;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Enums\FiltersLayout;
use Filament\Tables\Enums\FiltersResetActionPosition;
use Filament\Tables\Table;

/**
 * Shared Filament table defaults for the admin panel.
 */
final class AdminTable
{
    public static function configureUsing(Table $table): void
    {
        $table
            ->filtersLayout(FiltersLayout::AboveContent)
            ->filtersResetActionPosition(FiltersResetActionPosition::Footer)
            ->filtersFormSchema(function (array $filters): array {
                return collect($filters)
                    ->map(fn ($group) => $group->inlineLabel())
                    ->values()
                    ->all();
            })
            ->extraAttributes(['class' => 'lb-admin-table'], merge: true);
    }

    /**
     * Opt-in sticky column (typically id), pinned after the selection checkbox.
     */
    public static function stickyStart(Column $column): Column
    {
        return $column
            ->extraHeaderAttributes(['class' => 'lb-sticky-col-start'], merge: true)
            ->extraCellAttributes(['class' => 'lb-sticky-col-start'], merge: true);
    }

    /**
     * Long text: single-line CSS ellipsis (hover via native title when limited).
     */
    public static function ellipsis(TextColumn $column, int $clamp = 1): TextColumn
    {
        return $column
            ->wrap()
            ->lineClamp($clamp)
            ->tooltip(function (mixed $state): ?string {
                if (! is_string($state) || $state === '') {
                    return null;
                }

                return $state;
            });
    }
}
