<?php

namespace App\Filament\Resources\Campaigns\Schemas;

use App\Enums\CampaignStatus;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Repeater\TableColumn;
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Forms\Components\ToggleButtons;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Group;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Support\RawJs;

class CampaignForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Grid::make()
                    ->schema([
                        TextInput::make('data_attribute.name')
                            ->required()
                            ->maxLength(255),
                        TextInput::make('slug')
                            ->unique(ignoreRecord: true)
                            ->required()
                            ->regex('/^[a-z0-9-]+$/')
                            ->helperText('Only lowercase letters, numbers, and hyphens are allowed.')
                            ->maxLength(255),
                    ]),
                ToggleButtons::make('status')
                    ->inline()
                    ->default(CampaignStatus::Draft)
                    ->options(CampaignStatus::class),
                Group::make([
                    RichEditor::make('description')
                        ->maxLength(65535),
                    FileUpload::make('logo')
                        ->disk('public')
                        ->image()
                        ->previewable(true),
                    TextInput::make('redirect_url')
                        ->placeholder('https://example.com')
                        ->prefixIcon(Heroicon::OutlinedLink)
                        ->maxLength(255),
                ])->statePath('data_attribute'),
                Repeater::make('prices')
                    ->relationship()
                    ->table([
                        TableColumn::make('Name'),
                        TableColumn::make('Price'),
                        TableColumn::make('Default'),
                        TableColumn::make('Active'),
                    ])
                    ->schema([
                        TextInput::make('name')
                            ->maxLength(255),
                        TextInput::make('amount')
                            ->prefix('USD')
                            ->mask(RawJs::make('$money($input)'))
                            ->numeric(),
                        Toggle::make('is_default')
                            ->fixIndistinctState(),
                        Toggle::make('is_active')
                            ->default(true),
                    ]),
            ])->columns(1);
    }
}
