<?php

declare(strict_types=1);

namespace App\Filament\Resources\Rooms\Schemas;

use App\Enums\PricingMode;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\TimePicker;
use Filament\Forms\Components\Toggle;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Components\Utilities\Get;
use Filament\Schemas\Schema;

/**
 * Where the studio changes its own prices.
 *
 * Every rate and rule the booking engine consults lives on this record, so a
 * price change is a form entry rather than a deployment. The fields shown
 * follow the pricing mode, because a day-rate room has no hourly rate and an
 * hourly room has no day length — showing both would invite someone to fill
 * in a combination the engine cannot price.
 */
class RoomForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema->components([
            Section::make('The room')
                ->schema([
                    TextInput::make('name')->required()->maxLength(255),
                    TextInput::make('slug')
                        ->required()
                        ->maxLength(255)
                        ->unique(ignoreRecord: true)
                        ->helperText('Used in links. Changing it will break existing ones.'),
                    Textarea::make('description')->rows(2)->columnSpanFull(),
                    TextInput::make('capacity')
                        ->numeric()
                        ->suffix('people')
                        ->helperText('Studio D holds 10.'),
                    Toggle::make('is_active')
                        ->label('Bookable')
                        ->default(true)
                        ->helperText('Turning this off hides the room without touching existing bookings.'),
                ])
                ->columns(2),

            Section::make('How it is charged')
                ->schema([
                    Select::make('pricing_mode')
                        ->label('Billing')
                        ->options(PricingMode::class)
                        ->default(PricingMode::Hourly)
                        ->required()
                        ->live()
                        ->helperText('Hourly for Studios A, B and C. Whole days for Studio D.'),

                    TextInput::make('hourly_rate')
                        ->label('Per hour')
                        ->numeric()->prefix('£')->step(0.01)
                        ->visible(fn (Get $get) => $get('pricing_mode') === PricingMode::Hourly->value)
                        ->required(fn (Get $get) => $get('pricing_mode') === PricingMode::Hourly->value),

                    TextInput::make('hourly_rate_with_engineer')
                        ->label('Per hour, with an engineer')
                        ->numeric()->prefix('£')->step(0.01)
                        ->visible(fn (Get $get) => $get('pricing_mode') === PricingMode::Hourly->value)
                        ->helperText('Leave blank for a self-serve room like Studio C.'),

                    TextInput::make('min_hours')
                        ->label('Minimum session')
                        ->numeric()->suffix('hours')->default(2)
                        ->visible(fn (Get $get) => $get('pricing_mode') === PricingMode::Hourly->value)
                        ->required(),

                    TextInput::make('day_rate')
                        ->label('Per day')
                        ->numeric()->prefix('£')->step(0.01)
                        ->visible(fn (Get $get) => $get('pricing_mode') === PricingMode::DayRate->value)
                        ->required(fn (Get $get) => $get('pricing_mode') === PricingMode::DayRate->value),

                    TextInput::make('day_length_hours')
                        ->label('A day is')
                        ->numeric()->suffix('hours')
                        ->visible(fn (Get $get) => $get('pricing_mode') === PricingMode::DayRate->value)
                        ->required(fn (Get $get) => $get('pricing_mode') === PricingMode::DayRate->value),

                    TimePicker::make('day_earliest_start')
                        ->label('Earliest a day may start')
                        ->seconds(false)
                        ->visible(fn (Get $get) => $get('pricing_mode') === PricingMode::DayRate->value)
                        ->helperText('Leave blank to allow any start time.'),

                    TimePicker::make('day_latest_start')
                        ->label('Latest a day may start')
                        ->seconds(false)
                        ->visible(fn (Get $get) => $get('pricing_mode') === PricingMode::DayRate->value)
                        ->helperText('A 12 hour day from 15:00 runs to 03:00.'),
                ])
                ->columns(2),

            Section::make('Rules')
                ->schema([
                    Toggle::make('takes_engineer')
                        ->label('An engineer can work in here')
                        ->default(true)
                        ->helperText('Off for the podcast room — it is self-serve and never uses the rota.'),

                    Toggle::make('package_redeemable')
                        ->label('Package hours can be spent here')
                        ->default(true)
                        ->helperText('Off for Studio D: it is sold as a day or not at all.'),

                    TextInput::make('lead_time_hours')
                        ->label('Notice required')
                        ->numeric()->suffix('hours')->default(24)->required()
                        ->helperText('Label Ready holders are exempt from this.'),

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

                    TimePicker::make('opens_at')
                        ->seconds(false)
                        ->helperText('Leave both blank for round-the-clock booking.'),
                    TimePicker::make('closes_at')->seconds(false),
                ])
                ->columns(2),
        ]);
    }
}
