<?php

namespace App\Filament\Resources\Domains\Schemas;

use App\Models\Plan;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Schemas\Schema;

class DomainForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Select::make('user_id')
                    ->label('Legacy assigned user')
                    ->relationship('user', 'name')
                    ->hidden()
                    ->dehydrated(false),
                Select::make('plan_id')
                    ->label('Subscription plan / package')
                    ->options(fn (): array => Plan::query()
                        ->where('status', 'active')
                        ->where('custom_domain_limit', '>', 0)
                        ->orderBy('price')
                        ->pluck('name', 'id')
                        ->all())
                    ->searchable()
                    ->preload()
                    ->helperText('Only plans with 1+ custom domains are selectable. Free/0-domain plans use the default short domain only.'),
                TextInput::make('domain')
                    ->label('Short-link domain')
                    ->placeholder('go.example.com')
                    ->helperText('Enter only the host. https:// and paths are cleaned automatically.')
                    ->required(),
                Toggle::make('is_default')
                    ->label('Default short domain')
                    ->helperText('Default domain is free and available to every plan/package.')
                    ->required(),
                Select::make('status')
                    ->options([
                        'active' => 'Active',
                        'pending' => 'Pending',
                        'blocked' => 'Blocked',
                    ])
                    ->required()
                    ->default('active'),
                Select::make('ssl_status')
                    ->label('SSL status')
                    ->options([
                        'verified' => 'Verified',
                        'pending' => 'Pending',
                        'unknown' => 'Unknown',
                        'failed' => 'Failed',
                    ])
                    ->required()
                    ->default('unknown'),
                TextInput::make('verification_token')
                    ->disabled()
                    ->dehydrated(false)
                    ->helperText('Generated automatically when the domain is created.'),
            ]);
    }
}
