<?php

namespace App\Filament\Widgets;

use Filament\Tables;
use Filament\Widgets\TableWidget as BaseWidget;
use App\Models\Photo;
use Filament\Actions;

class RecentPhotosTable extends BaseWidget
{
    protected static ?string $heading = 'Recent uploads';
    protected int|string|array $columnSpan = 'full';

    public function table(Tables\Table $table): Tables\Table
    {
        return $table
            ->query(
                Photo::query()
                    ->orderByDesc('created_at')
                    ->limit(10)
            )
            ->columns([
                Tables\Columns\ImageColumn::make('thumbnail_path')->label('Thumb')->circular(),
                Tables\Columns\TextColumn::make('id')->label('#'),
                Tables\Columns\TextColumn::make('event_id')->label('Event'),
                Tables\Columns\TextColumn::make('likes_count')->label('Likes'),
                Tables\Columns\TextColumn::make('created_at')->since(),
            ])
            ->actions([
                Actions\Action::make('feature')
                    ->label('Feature')
                    ->visible(fn(Photo $record) => ! (bool)($record->is_featured ?? 0))
                    ->action(fn(Photo $record) => $record->update(['is_featured' => 1])),
                Actions\Action::make('unfeature')
                    ->label('Unfeature')
                    ->visible(fn(Photo $record) => (bool)($record->is_featured ?? 0))
                    ->action(fn(Photo $record) => $record->update(['is_featured' => 0])),
            ])
            ->paginated(false);
    }
}
