<?php

namespace App\Filament\Resources\Banners\Schemas;

use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Schemas\Schema;

class BannerForm
{
    /** Ad spaces available in the customer app. */
    public const PLACEMENTS = [
        'home_top' => 'Home — top (below the search bar)',
        'home_mid' => 'Home — middle (between sections)',
    ];

    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                TextInput::make('title')
                    ->required()
                    ->helperText('For your own reference (also used as the image alt text).'),

                Select::make('placement')
                    ->label('Ad space')
                    ->required()
                    ->default('home_top')
                    ->options(self::PLACEMENTS)
                    ->helperText('Where this banner appears in the customer app.'),

                FileUpload::make('image')
                    ->label('Banner image')
                    ->image()
                    ->disk('public')
                    ->directory('banners')
                    ->required()
                    ->helperText('Wide banner, ~1200×500px works best (JPG or PNG).')
                    ->columnSpanFull(),

                Select::make('link_type')
                    ->label('When tapped')
                    ->default('none')
                    ->live()
                    ->options([
                        'none'     => 'Do nothing (just show the image)',
                        'url'      => 'Open a website',
                        'category' => 'Open a category',
                        'product'  => 'Open a product',
                        'vendor'   => 'Open a store',
                    ]),

                TextInput::make('link_value')
                    ->label(fn($get) => match ($get('link_type')) {
                        'url'      => 'Website URL',
                        'category' => 'Category slug',
                        'product'  => 'Product slug',
                        'vendor'   => 'Store slug',
                        default    => 'Link',
                    })
                    ->visible(fn($get) => ($get('link_type') ?? 'none') !== 'none')
                    ->required(fn($get) => ($get('link_type') ?? 'none') !== 'none')
                    ->helperText(fn($get) => match ($get('link_type')) {
                        'url'                          => 'e.g. https://example.com',
                        'category', 'product', 'vendor' => 'The slug from the end of its page URL.',
                        default                        => null,
                    }),

                Toggle::make('is_active')
                    ->label('Active')
                    ->default(true)
                    ->helperText('Turn this ad space on or off.'),

                TextInput::make('sort_order')
                    ->numeric()
                    ->default(0)
                    ->helperText('Lower numbers show first when a space has several banners.'),

                DateTimePicker::make('starts_at')
                    ->label('Start showing on (optional)'),

                DateTimePicker::make('ends_at')
                    ->label('Stop showing on (optional)'),
            ]);
    }
}
