<?php

namespace App\Filament\Resources;

use App\Filament\Resources\PhotoResource\Pages;
use App\Models\Photo;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use UnitEnum;
use BackedEnum;
use Filament\Actions;

class PhotoResource extends Resource
{
    protected static ?string $model = Photo::class;
    protected static BackedEnum|string|null $navigationIcon = 'heroicon-o-photo';
    protected static UnitEnum|string|null $navigationGroup = 'Content';
    protected static ?int $navigationSort = 30;

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\ImageColumn::make('thumbnail_path')->label('Thumb')->circular(),
                Tables\Columns\TextColumn::make('id')->sortable(),
                Tables\Columns\TextColumn::make('event_id')->label('Event'),
                Tables\Columns\TextColumn::make('likes_count')->label('Likes'),
                Tables\Columns\IconColumn::make('is_featured')->boolean(),
                Tables\Columns\TextColumn::make('created_at')->since(),
            ])
            ->filters([])
            ->actions([
                Actions\EditAction::make(),
                Actions\Action::make('feature')
                    ->label('Feature')
                    ->visible(fn($record) => !$record->is_featured)
                    ->action(fn($record) => $record->update(['is_featured' => true]))
                    ->icon('heroicon-o-star'),
                Actions\Action::make('unfeature')
                    ->label('Unfeature')
                    ->visible(fn($record) => $record->is_featured)
                    ->action(fn($record) => $record->update(['is_featured' => false]))
                    ->icon('heroicon-o-star'),
                Actions\DeleteAction::make(),
            ])
            ->bulkActions([
                Actions\BulkAction::make('feature')
                    ->label('Feature selected')
                    ->icon('heroicon-o-star')
                    ->action(fn($records) => $records->each->update(['is_featured' => true])),
                Actions\BulkAction::make('unfeature')
                    ->label('Unfeature selected')
                    ->icon('heroicon-o-star')
                    ->action(fn($records) => $records->each->update(['is_featured' => false])),
                Actions\DeleteBulkAction::make(),
            ]);
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListPhotos::route('/'),
            'view' => Pages\ViewPhoto::route('/{record}'),
            'edit' => Pages\EditPhoto::route('/{record}/edit'),
        ];
    }
}