<?php

namespace App\Filament\Resources\Plans\Schemas;

use Filament\Actions\Action;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Repeater\TableColumn;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Schemas\Components\Callout;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Components\Tabs;
use Filament\Schemas\Components\Tabs\Tab;
use Filament\Schemas\Schema;

class PlanForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Tabs::make('Pricing package CMS')
                    ->persistTabInQueryString('plan_tab')
                    ->columnSpanFull()
                    ->tabs([
                        Tab::make('Package')
                            ->icon('heroicon-m-credit-card')
                            ->schema([
                                Callout::make('Admin-managed pricing')
                                    ->info()
                                    ->description('These package values power homepage pricing cards, the user subscription page, and Stripe Checkout. Use 0 custom domains for free/basic plans.'),

                                Section::make('Plan identity')
                                    ->columns(2)
                                    ->schema([
                                        TextInput::make('name')
                                            ->required()
                                            ->maxLength(255),

                                        Select::make('status')
                                            ->options([
                                                'active' => 'Active',
                                                'archived' => 'Archived',
                                            ])
                                            ->native(false)
                                            ->required()
                                            ->default('active'),

                                        Textarea::make('description')
                                            ->label('Pricing card description')
                                            ->rows(4)
                                            ->maxLength(500)
                                            ->helperText('Shown on pricing cards. Leave empty to use the default copy.')
                                            ->columnSpanFull(),

                                        TextInput::make('card_badge_label')
                                            ->label('Ribbon text')
                                            ->placeholder('Most popular')
                                            ->helperText('Shown as the pricing card ribbon. If empty, featured plans use “Popular”.'),

                                        TextInput::make('ribbon_bg_color')
                                            ->label('Ribbon background color')
                                            ->type('color')
                                            ->default('#4648d4'),

                                        TextInput::make('ribbon_text_color')
                                            ->label('Ribbon text color')
                                            ->type('color')
                                            ->default('#ffffff'),
                                    ]),

                                Section::make('Billing prices')
                                    ->description('Discount percentages are applied to checkout and public pricing. Keep them 0 for no discount.')
                                    ->columns(2)
                                    ->schema([
                                        TextInput::make('price')
                                            ->label('Monthly price')
                                            ->required()
                                            ->numeric()
                                            ->default(0)
                                            ->prefix('$'),

                                        TextInput::make('yearly_price')
                                            ->label('Yearly base price')
                                            ->numeric()
                                            ->prefix('$')
                                            ->helperText('Optional. Leave empty to charge 12 months of the monthly price before yearly discount.'),

                                        TextInput::make('monthly_discount_percent')
                                            ->label('Monthly discount')
                                            ->numeric()
                                            ->minValue(0)
                                            ->maxValue(100)
                                            ->default(0)
                                            ->suffix('%'),

                                        TextInput::make('yearly_discount_percent')
                                            ->label('Yearly discount')
                                            ->numeric()
                                            ->minValue(0)
                                            ->maxValue(100)
                                            ->default(0)
                                            ->suffix('%'),
                                    ]),

                                Section::make('Usage limits')
                                    ->columns(2)
                                    ->schema([
                                        TextInput::make('link_limit')
                                            ->numeric()
                                            ->helperText('Leave empty for unlimited links.'),

                                        TextInput::make('custom_domain_limit')
                                            ->label('Custom domains included')
                                            ->required()
                                            ->numeric()
                                            ->default(0)
                                            ->helperText('Use 0 for free/basic packages. Plans with 0 cannot use custom short-link domains.'),

                                        TextInput::make('analytics_retention_days')
                                            ->required()
                                            ->numeric()
                                            ->default(30),

                                        Grid::make(2)
                                            ->schema([
                                                Toggle::make('ad_feature_enabled')
                                                    ->label('Monetization enabled')
                                                    ->required(),

                                                Toggle::make('api_access')
                                                    ->label('API access')
                                                    ->required(),
                                            ]),
                                    ]),
                            ]),

                        Tab::make('Card CMS')
                            ->icon('heroicon-m-sparkles')
                            ->schema([
                                Section::make('Pricing card details')
                                    ->description('Fully customize how this package appears on homepage and user subscription cards. The first 2 public feature rows are generated by the system: short-link allowance and package duration.')
                                    ->columns(2)
                                    ->schema([
                                        TextInput::make('cta_label')
                                            ->label('Button label')
                                            ->placeholder('Choose plan'),

                                        Toggle::make('is_featured')
                                            ->label('Featured pricing card')
                                            ->helperText('Highlights this package on pricing cards.'),

                                        TextInput::make('sort_order')
                                            ->label('Display order')
                                            ->numeric()
                                            ->default(0)
                                            ->helperText('Lower numbers appear first.'),

                                        Repeater::make('card_features')
                                            ->label('Plan Features CMS')
                                            ->helperText('Add feature row 3 and onward here. The Icon column supports Tick - green and Cross - red.')
                                            ->table([
                                                TableColumn::make('Icon')
                                                    ->width('12rem')
                                                    ->markAsRequired(),
                                                TableColumn::make('Features')
                                                    ->markAsRequired(),
                                            ])
                                            ->compact()
                                            ->schema([
                                                Select::make('icon')
                                                    ->label('Icon')
                                                    ->options([
                                                        'check' => 'Tick - green',
                                                        'close' => 'Cross - red',
                                                    ])
                                                    ->native(false)
                                                    ->required()
                                                    ->default('check')
                                                    ->hiddenLabel(),

                                                TextInput::make('text')
                                                    ->label('Feature text')
                                                    ->required()
                                                    ->maxLength(160)
                                                    ->placeholder('3 custom domains included')
                                                    ->hiddenLabel(),
                                            ])
                                            ->addAction(fn (Action $action): Action => $action
                                                ->label('Add New')
                                                ->icon('heroicon-m-plus-circle')
                                                ->color('primary')
                                                ->button())
                                            ->reorderable()
                                            ->reorderableWithButtons()
                                            ->defaultItems(0)
                                            ->columnSpanFull(),
                                    ]),
                            ]),
                    ]),
            ]);
    }
}
