<?php

namespace App\Filament\Resources\Sliders;

use App\Filament\Resources\Sliders\Pages\ManageSliders;
use App\Models\Slider;
use BackedEnum;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteAction;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\ImageColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;

use Filament\Forms\Components\Select;
use Filament\Schemas\Components\Section;

class SliderResource extends Resource
{
    protected static ?string $model = Slider::class;

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

    protected static string|\UnitEnum|null $navigationGroup = 'Site Builder';

    protected static ?int $navigationSort = 3;

    protected static ?string $recordTitleAttribute = 'title';

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                // 1. Slider Content Section (Vertical Stack with 2 columns)
                Section::make('📝 Slider Content')
                    ->description('Configure the headings, promotional badge, and call-to-action button details.')
                    ->schema([
                        TextInput::make('title')
                            ->label('Main Title')
                            ->placeholder('e.g. Premium Honey Collection')
                            ->maxLength(255)
                            ->columnSpan(2),

                        TextInput::make('subtitle')
                            ->label('Sub-title / Description')
                            ->placeholder('e.g. 100% pure and natural directly from hives')
                            ->maxLength(255)
                            ->columnSpan(2),

                        TextInput::make('badge')
                            ->label('Promo Badge Text')
                            ->placeholder('e.g. 100% PURE')
                            ->maxLength(50),

                        TextInput::make('btn_text')
                            ->label('Button CTA Label')
                            ->placeholder('e.g. Order Now')
                            ->maxLength(50),

                        TextInput::make('link_url')
                            ->label('Redirect link / URL')
                            ->placeholder('e.g. /shop?category=honey')
                            ->url()
                            ->maxLength(255)
                            ->columnSpan(2),
                    ])
                    ->columns(2),

                // 2. Styling & Placement Section (Vertical Stack with 2 columns)
                Section::make('🎨 Styling & Placement')
                    ->description('Upload image and select where this banner should display.')
                    ->schema([
                        FileUpload::make('image')
                            ->label('Banner Image')
                            ->disk(config('filesystems.default', 'public'))
                            ->directory('sliders')
                            ->image()
                            ->imageEditor()
                            ->required()
                            ->columnSpan(2)
                            ->helperText(fn ($get) => match($get('type')) {
                                'hero'      => '📐 Recommended size: 1860 × 744 px (2x for High Quality)',
                                'hero_side' => '📐 Recommended size: 910 × 744 px (2x for High Quality)',
                                'middle'    => '📐 Recommended size: 2400 × 720 px (2x for High Quality)',
                                default     => '📐 Select a type first to see recommended size',
                            }),

                        Select::make('type')
                            ->label('Storefront Position')
                            ->options([
                                'hero'      => 'Hero Main Carousel (Top Left)',
                                'hero_side' => 'Hero Side Panel (Top Right)',
                                'middle'    => 'Middle Promotional Banner',
                            ])
                            ->default('hero')
                            ->required()
                            ->live()
                            ->helperText(fn ($get) => match($get('type')) {
                                'hero'      => 'Displays in main sliding banner (Top Left).',
                                'hero_side' => 'Displays in smaller side panel (Top Right).',
                                'middle'    => 'Displays wide banner after 3rd product section.',
                                default     => '',
                            }),

                        TextInput::make('bg_gradient')
                            ->label('Background Fallback CSS Class')
                            ->placeholder('e.g. bg-gradient-to-r from-green-900 to-emerald-950')
                            ->maxLength(255),

                        TextInput::make('sort_order')
                            ->label('Display Order')
                            ->numeric()
                            ->default(0)
                            ->required(),

                        Toggle::make('is_active')
                            ->label('Active Storefront')
                            ->default(true)
                            ->inline(false)
                            ->required(),
                    ])
                    ->columns(2),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->recordTitleAttribute('title')
            ->columns([
                TextColumn::make('title')
                    ->searchable(),
                TextColumn::make('subtitle')
                    ->searchable(),
                TextColumn::make('badge')
                    ->searchable(),
                TextColumn::make('btn_text')
                    ->searchable(),
                TextColumn::make('link_url')
                    ->searchable(),
                ImageColumn::make('image'),
                TextColumn::make('bg_gradient')
                    ->searchable(),
                TextColumn::make('type')
                    ->searchable(),
                TextColumn::make('sort_order')
                    ->numeric()
                    ->sortable(),
                IconColumn::make('is_active')
                    ->boolean(),
                TextColumn::make('created_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('updated_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                //
            ])
            ->recordActions([
                EditAction::make(),
                DeleteAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }

    public static function getPages(): array
    {
        return [
            'index' => ManageSliders::route('/'),
        ];
    }
}
