<?php

namespace App\Filament\Clusters\Settings\Branches\Schemas;

use AbdulmajeedJamaan\FilamentTranslatableTabs\TranslatableTabs;
use App\Modules\Province\Models\Province;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;

class BranchForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make(__('General Information'))
                    ->columns(2)
                    ->schema([
                        TextInput::make('unique_code')
                            ->label(__('Unique code'))
                            ->maxLength(255)
                            ->unique(ignoreRecord: true)
                            ->required(),

                        Select::make('region')
                            ->label(__('Region'))
                            ->options(fn () => regions())
                            ->required()
                            ->native(false)
                            ->searchable()
                            ->live()
                            ->afterStateUpdated(fn ($state, callable $set) => $set('province_id', null)),

                        Select::make('province_id')
                            ->label(__('Province'))
                            ->relationship('province', 'name')
                            ->options(function (callable $get) {
                                $region = $get('region');
                                if (! $region) {
                                    return [];
                                }

                                return Province::query()
                                    ->where('region', $region)
                                    ->where('active', true)
                                    ->pluck('name', 'id')
                                    ->toArray();
                            })
                            ->searchable()
                            ->native(false)
                            ->disabled(fn (callable $get): bool => ! $get('region'))
                            ->columnSpanFull(),

                        Toggle::make('active')
                            ->label(__('Active'))
                            ->default(true)
                            ->required(),

                        Repeater::make('phone_numbers')
                            ->label(__('Phone numbers'))
                            ->simple(
                                TextInput::make('phone')
                                    ->label(__('Phone Number'))
                                    ->prefix('+993')
                                    ->numeric()
                            )
                            ->addActionLabel(__('Add phone number'))
                            ->defaultItems(0)
                            ->columnSpanFull(),
                    ]),

                TranslatableTabs::make()
                    ->schema([
                        TextInput::make('name')
                            ->label(__('Name'))
                            ->required()
                            ->maxLength(255),

                        Textarea::make('address')
                            ->label(__('Address'))
                            ->rows(3),

                    ]),

                Section::make(__('Billing Credentials'))
                    ->collapsed()
                    ->collapsible()
                    ->columns(2)
                    ->columnSpanFull()
                    ->schema([
                        TextInput::make('billing_username')
                            ->label(__('Billing Username'))
                            ->maxLength(255),

                        TextInput::make('billing_password')
                            ->label(__('Billing Password'))
                            ->password()
                            ->maxLength(255),

                        TextInput::make('billing_swift_username')
                            ->label(__('Billing SWIFT Username'))
                            ->maxLength(255),

                        TextInput::make('billing_swift_password')
                            ->label(__('Billing SWIFT Password'))
                            ->password()
                            ->maxLength(255),

                        TextInput::make('billing_visa_master_username')
                            ->label(__('Billing Visa/Master Username'))
                            ->maxLength(255),

                        TextInput::make('billing_visa_master_password')
                            ->label(__('Billing Visa/Master Password'))
                            ->password()
                            ->maxLength(255),

                        TextInput::make('billing_sber_username')
                            ->label(__('Billing Sber Username'))
                            ->maxLength(255),

                        TextInput::make('billing_sber_password')
                            ->label(__('Billing Sber Password'))
                            ->password()
                            ->maxLength(255),
                    ]),
            ]);
    }
}
