<?php

namespace App\Filament\Resources;

use App\Models\HeroImage;
use App\Filament\Resources\HeroImageResource\Pages;
use Filament\Forms;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Schemas\Components\Section;
use Filament\Tables;
use Filament\Tables\Table;

class HeroImageResource extends Resource
{
    protected static ?string $model = HeroImage::class;

    protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-photo';

    protected static ?string $navigationLabel = 'Hero Images';

    protected static ?string $modelLabel = 'Hero Image';

    protected static ?string $pluralModelLabel = 'Hero Images';

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->schema([
                Forms\Components\Select::make('page')
                    ->label('Page')
                    ->options(HeroImage::getPages())
                    ->required()
                    ->helperText('Choose which page this hero image will appear on'),

                Forms\Components\FileUpload::make('image_path')
                    ->label('Hero Image')
                    ->image()
                    ->required()
                    ->disk('public')
                    ->directory('hero-images')
                    ->maxSize(5120)
                    ->acceptedFileTypes(['image/jpeg', 'image/png', 'image/webp', 'image/gif'])
                    ->helperText('Upload a high-quality image (max 5MB). Recommended size: 1920x1080px'),

                Forms\Components\TextInput::make('title')
                    ->label('Heading')
                    ->required()
                    ->maxLength(255)
                    ->helperText('Main heading text displayed on the hero image'),

                Forms\Components\Textarea::make('description')
                    ->label('Description')
                    ->maxLength(500)
                    ->rows(3)
                    ->helperText('Subheading text displayed below the title'),

                Forms\Components\TextInput::make('button_text')
                    ->label('Button Text')
                    ->maxLength(100)
                    ->helperText('Text for the call-to-action button (optional)'),

                Forms\Components\TextInput::make('button_link')
                    ->label('Button Link')
                    ->url()
                    ->maxLength(255)
                    ->helperText('URL or page anchor (e.g., #products or /products)'),

                Forms\Components\TextInput::make('sort_order')
                    ->label('Sort Order')
                    ->numeric()
                    ->default(0)
                    ->helperText('Lower numbers appear first in the slider'),

                Forms\Components\Toggle::make('is_active')
                    ->label('Active')
                    ->default(true)
                    ->helperText('Disable to hide this image from the website'),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('page')
                    ->label('Page')
                    ->sortable()
                    ->formatStateUsing(fn($state) => HeroImage::getPages()[$state] ?? $state)
                    ->badge(),

                Tables\Columns\ImageColumn::make('image_path')
                    ->label('Image')
                    ->size(80),

                Tables\Columns\TextColumn::make('title')
                    ->label('Title')
                    ->searchable()
                    ->limit(50),

                Tables\Columns\TextColumn::make('sort_order')
                    ->label('Order')
                    ->sortable()
                    ->numeric(),

                Tables\Columns\IconColumn::make('is_active')
                    ->label('Active')
                    ->boolean()
                    ->sortable(),

                Tables\Columns\TextColumn::make('created_at')
                    ->label('Created')
                    ->dateTime('M d, Y')
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                Tables\Filters\SelectFilter::make('page')
                    ->options(HeroImage::getPages()),

                Tables\Filters\TernaryFilter::make('is_active')
                    ->label('Active')
                    ->native(false),
            ])
            ->actions([
                //
            ])
            ->bulkActions([
                //
            ])
            ->defaultSort('page', 'asc')
            ->defaultSort('sort_order', 'asc');
    }

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

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