<?php

namespace App\Filament\Resources;

use App\Filament\Resources\MediaResource\Pages;
use App\Filament\Resources\MediaResource\RelationManagers;
use App\Models\Media;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;

class MediaResource extends Resource
{
    protected static ?string $model = Media::class;

    protected static ?string $navigationIcon = 'heroicon-o-photo';
    protected static ?string $navigationGroup = 'Media';
    protected static ?string $navigationLabel = 'Media Library';
    protected static ?int $navigationSort = 10;

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\Section::make('Upload / replace image')
                    ->description('Upload images that can be selected for sections or shown in the homepage gallery.')
                    ->schema([
                        Forms\Components\Hidden::make('disk')
                            ->default('public')
                            ->required(),
                        Forms\Components\FileUpload::make('path')
                            ->label('Image file')
                            ->disk('public')
                            ->directory('media')
                            ->image()
                            ->imageEditor()
                            ->openable()
                            ->downloadable()
                            ->storeFileNamesIn('filename')
                            ->required()
                            ->helperText('Upload or replace the image file.'),
                        Forms\Components\TextInput::make('filename')
                            ->label('Original filename')
                            ->required()
                            ->helperText('Automatically filled when uploading, but can be edited if needed.'),
                    ]),

                Forms\Components\Section::make('Image details')
                    ->description('These details improve accessibility and gallery captions.')
                    ->schema([
                        Forms\Components\TextInput::make('alt_text')
                            ->label('Alt text')
                            ->helperText('Describe the image for accessibility.'),
                        Forms\Components\TextInput::make('caption')
                            ->label('Caption')
                            ->helperText('Short caption shown over the image in the homepage gallery.'),
                    ])
                    ->columns(2),

                Forms\Components\Section::make('Homepage gallery')
                    ->description('Control whether this image appears in the Life at Hillcrest Living scrolling gallery.')
                    ->schema([
                        Forms\Components\Toggle::make('show_on_home_gallery')
                            ->label('Show in homepage gallery')
                            ->helperText('Turn on to include this image in the homepage scrolling gallery.')
                            ->default(false),
                        Forms\Components\TextInput::make('home_gallery_sort_order')
                            ->label('Gallery display order')
                            ->numeric()
                            ->default(0)
                            ->helperText('Lower numbers appear first.'),
                    ])
                    ->columns(2),

                Forms\Components\Section::make('Technical metadata')
                    ->collapsed()
                    ->schema([
                        Forms\Components\TextInput::make('mime_type')
                            ->label('MIME type'),
                        Forms\Components\TextInput::make('size_bytes')
                            ->label('Size (bytes)')
                            ->numeric(),
                        Forms\Components\TextInput::make('width')
                            ->numeric(),
                        Forms\Components\TextInput::make('height')
                            ->numeric(),
                        Forms\Components\TextInput::make('uploaded_by')
                            ->numeric(),
                    ])
                    ->columns(3),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('disk')
                    ->searchable(),
                Tables\Columns\TextColumn::make('filename')
                    ->label('File')
                    ->searchable(),
                Tables\Columns\TextColumn::make('caption')
                    ->searchable(),
                Tables\Columns\IconColumn::make('show_on_home_gallery')
                    ->label('Homepage gallery')
                    ->boolean(),
                Tables\Columns\TextColumn::make('home_gallery_sort_order')
                    ->label('Gallery order')
                    ->sortable(),
                Tables\Columns\TextColumn::make('mime_type')
                    ->searchable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('created_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('updated_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                Tables\Filters\TernaryFilter::make('show_on_home_gallery')
                    ->label('Homepage gallery'),
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
                Tables\Actions\DeleteAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListMedia::route('/'),
            'create' => Pages\CreateMedia::route('/create'),
            'edit' => Pages\EditMedia::route('/{record}/edit'),
        ];
    }
}
