<?php

declare(strict_types=1);


namespace App\Filament\Resources\Media\Tables;

use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Tables\Columns\ImageColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;

class MediaTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                ImageColumn::make('preview')
                    ->label('预览')
                    ->state(fn ($record) => $record->getCustomProperty('isimage') ? $record->getUrl() : null)
                    ->circular()
                    ->defaultImageUrl('/images/file-icon.png'),
                TextColumn::make('file_name')
                    ->label('文件名')
                    ->searchable()
                    ->url(fn ($record) => $record->getUrl())
                    ->openUrlInNewTab(),
                TextColumn::make('collection_name')
                    ->label('用途')
                    ->badge()
                    ->formatStateUsing(fn ($state) => match ($state) {
                        'cover' => '封面',
                        'attachments' => '附件',
                        default => $state,
                    }),
                TextColumn::make('model_type')
                    ->label('关联')
                    ->formatStateUsing(fn ($state) => class_basename((string) $state)),
                TextColumn::make('model_id')
                    ->label('ID'),
                TextColumn::make('size')
                    ->label('大小')
                    ->formatStateUsing(fn ($state) => number_format((float) $state / 1024, 1).' KB'),
                TextColumn::make('custom_properties.downloads')
                    ->label('下载'),
                TextColumn::make('created_at')
                    ->label('上传时间')
                    ->dateTime('Y-m-d H:i'),
            ])
            ->filters([
                \Filament\Tables\Filters\SelectFilter::make('collection_name')
                    ->label('用途')
                    ->options([
                        'attachments' => '附件',
                        'cover' => '封面',
                    ]),
            ], \Filament\Tables\Enums\FiltersLayout::AboveContent)
            ->recordActions([
                \Filament\Actions\EditAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ])
            ->defaultSort('created_at', 'desc');
    }
}
