<?php

namespace App\Filament\App\Pages;

use App\Services\OnboardingService;
use Filament\Facades\Filament;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Filament\Support\Icons\Heroicon;

class Onboarding extends Page
{
    protected string $view = 'filament.app.pages.onboarding';

    protected static string|\BackedEnum|null $navigationIcon = Heroicon::OutlinedSparkles;

    protected static ?string $title = 'First Setup';

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

    protected static ?int $navigationSort = -2;

    protected static string $routePath = '/onboarding';

    public string $cashAccountName = 'Cash Wallet';

    public float|string $cashOpeningBalance = 0;

    public bool $includeBankAccount = true;

    public string $bankAccountName = 'Primary Bank';

    public float|string $bankOpeningBalance = 0;

    public string $effectiveDate = '';

    public function mount(): void
    {
        $this->effectiveDate = now()->toDateString();
    }

    public function save(): void
    {
        $user = Filament::auth()->user();

        app(OnboardingService::class)->complete($user, [
            'cash_account_name' => $this->cashAccountName,
            'cash_opening_balance' => (float) $this->cashOpeningBalance,
            'include_bank_account' => $this->includeBankAccount,
            'bank_account_name' => $this->bankAccountName,
            'bank_opening_balance' => (float) $this->bankOpeningBalance,
            'effective_date' => $this->effectiveDate,
        ]);

        Notification::make()
            ->success()
            ->title('Setup completed')
            ->body('Accounts, opening balances, and starter categories are ready.')
            ->send();
    }

    public function getStatusDataProperty(): array
    {
        return app(OnboardingService::class)->status(Filament::auth()->user());
    }
}
