<?php

namespace App\Filament\Resources;

use App\Filament\Resources\SchoolGalleryResource\Pages;
use App\Models\SchoolGallery;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;

class SchoolGalleryResource extends Resource
{
    protected static ?string $model = SchoolGallery::class;

    protected static ?string $navigationIcon = 'heroicon-o-photo';

    protected static ?string $label = 'School Gallery';
    protected static ?string $pluralLabel = 'School Galleries';

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\Select::make('school_id')
                    ->label('School')
                    ->relationship('school', 'name_en')
                    ->required()
                    ->searchable(),
                Forms\Components\Select::make('type')
                    ->label('Media Type')
                    ->options([
                        'image' => 'Image',
                        'video' => 'Video',
                        // 'pdf' => 'PDF',
                    ])
                    ->required(),
                Forms\Components\FileUpload::make('file')
                    ->label('File')
                    ->required()
                    ->directory('school-gallery')
                    ->acceptedFileTypes(['image/*', 'video/mp4'])
                    ->imageEditor(fn($state) => in_array($state['type'] ?? 'image', ['image'])),
                Forms\Components\TextInput::make('title_en')
                    ->label('Title (English)')
                    ->required()
                    ->maxLength(255),
                Forms\Components\TextInput::make('title_ar')
                    ->label('Title (Arabic)')
                    ->required()
                    ->maxLength(255),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('school.name_en')
                    ->label('School')
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('type')
                    ->label('Media Type')
                    ->sortable(),
                Tables\Columns\ImageColumn::make('file')
                    ->label('File')
                    ->visible(fn($record) => $record && $record->type === 'image'),
                Tables\Columns\TextColumn::make('title_en')
                    ->label('Title (English)')
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('created_at')
                    ->label('Created At')
                    ->dateTime()
                    ->sortable(),
            ])
            ->filters([
                Tables\Filters\SelectFilter::make('type')
                    ->options([
                        'image' => 'Image',
                        'video' => 'Video',
                        // 'pdf' => 'PDF',
                    ]),
            ])
            ->actions([
                Tables\Actions\ViewAction::make(),
                Tables\Actions\EditAction::make(),
                Tables\Actions\DeleteAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\DeleteBulkAction::make(),
            ]);
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListSchoolGalleries::route('/'),
            'create' => Pages\CreateSchoolGallery::route('/create'),
            // 'view' => Pages\ViewSchoolGallery::route('/{record}'),
            'edit' => Pages\EditSchoolGallery::route('/{record}/edit'),
        ];
    }
}
