<?php

namespace App\Filament\Pages;

use App\Models\Setting;
use Filament\Pages\Page;
use Filament\Schemas\Schema;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Textarea;
use Filament\Schemas\Components\Section;
use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Actions\Contracts\HasActions;
use Filament\Actions\Concerns\InteractsWithActions;
use Filament\Notifications\Notification;
use Filament\Actions\Action;

class ResellerSettings extends Page implements HasForms, HasActions
{
    use InteractsWithForms, InteractsWithActions;

    protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-credit-card';

    protected string $view = 'filament.pages.reseller-settings';

    protected static string|\UnitEnum|null $navigationGroup = 'Reseller System';

    protected static ?string $title = 'Reseller Payment Settings';

    protected static ?string $navigationLabel = 'Payment Settings';

    protected static ?int $navigationSort = 4;

    public ?array $data = [];

    public function mount()
    {
        $settings = Setting::pluck('value', 'key')->toArray();
        $this->form->fill([
            'reseller_bkash_number' => $settings['reseller_bkash_number'] ?? '',
            'reseller_nagad_number' => $settings['reseller_nagad_number'] ?? '',
            'reseller_rocket_number' => $settings['reseller_rocket_number'] ?? '',
            'reseller_payment_instructions' => $settings['reseller_payment_instructions'] ?? '',
        ]);
    }

    public function form(Schema $form): Schema
    {
        return $form
            ->schema([
                Section::make('Reseller Membership Payment Configurations')
                    ->description('Configure personal mobile banking numbers that resellers will see and pay to during account registration.')
                    ->schema([
                        TextInput::make('reseller_bkash_number')
                            ->label('bKash Number (Personal)')
                            ->placeholder('e.g. 017XXXXXXXX')
                            ->tel(),
                        TextInput::make('reseller_nagad_number')
                            ->label('Nagad Number (Personal)')
                            ->placeholder('e.g. 018XXXXXXXX')
                            ->tel(),
                        TextInput::make('reseller_rocket_number')
                            ->label('Rocket Number (Personal)')
                            ->placeholder('e.g. 019XXXXXXXX')
                            ->tel(),
                        Textarea::make('reseller_payment_instructions')
                            ->label('Send Money Instructions / Additional Guidelines (Optional)')
                            ->placeholder('e.g. Please save the Transaction ID after sending the money.')
                            ->rows(3)
                            ->columnSpanFull(),
                    ])
                    ->columns(3)
            ])
            ->statePath('data');
    }

    public function save()
    {
        $state = $this->form->getState();
        foreach ($state as $key => $value) {
            Setting::setValue($key, $value, 'reseller');
        }
        Setting::clearCache();

        Notification::make()
            ->title('Reseller payment settings updated successfully.')
            ->success()
            ->send();
    }

    protected function getFormActions(): array
    {
        return [
            Action::make('save')
                ->label('Save Settings')
                ->submit('save')
                ->color('primary')
                ->icon('heroicon-m-check'),
        ];
    }
}
