<?php

namespace App\Filament\Pages;

use Filament\Pages\Page;
use App\Services\AccountingClosingService;
use BezhanSalleh\FilamentShield\Traits\HasPageShield;
use Filament\Forms\Components\Select;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Notifications\Notification;
use Filament\Schemas\Schema;

class YearEndClosePage extends Page implements HasForms
{
    use InteractsWithForms, HasPageShield;

    protected static \BackedEnum|string|null $navigationIcon = 'heroicon-o-lock-closed';

    protected string $view = 'filament.pages.year-end-close-page';

    protected static string|\UnitEnum|null $navigationGroup = 'الحسابات الختامية';

    protected static ?int $navigationSort = 11;

    public ?int $year = null;
    public array $checkResult = [];

    protected AccountingClosingService $closingService;

    public function boot(AccountingClosingService $closingService): void
    {
        $this->closingService = $closingService;
    }

    public function mount(): void
    {
        $this->year = now()->year;
        $this->form->fill(['year' => $this->year]);
        $this->checkCanClose();
    }

    public static function getNavigationLabel(): string
    {
        return __('filament/admin/year_end_close_page.navigation_label');
    }

    public function getTitle(): string
    {
        return __('filament/admin/year_end_close_page.title');
    }

    public function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                Select::make('year')
                    ->label(__('filament/admin/year_end_close_page.year'))
                    ->options([
                        now()->year - 2 => now()->year - 2,
                        now()->year - 1 => now()->year - 1,
                        now()->year => now()->year,
                        now()->year + 1 => now()->year + 1,
                    ])
                    ->required()
                    ->live()
                    ->afterStateUpdated(function ($state) {
                        $this->year = $state;
                        $this->checkCanClose();
                    }),
            ]);
    }

    public function checkCanClose(): void
    {
        $this->checkResult = $this->closingService->canClose('yearly', $this->year);
    }

    public function performClose(): void
    {
        try {
            $result = $this->closingService->yearEndClose($this->year);
            
            Notification::make()
                ->title(__('filament/admin/year_end_close_page.success_title'))
                ->success()
                ->body(__('filament/admin/year_end_close_page.success_body', ['year' => $this->year, 'entry' => $result['entry_number']]))
                ->send();

            $this->checkCanClose();
        } catch (\Exception $e) {
            Notification::make()
                ->title(__('filament/admin/year_end_close_page.failed_title'))
                ->danger()
                ->body($e->getMessage())
                ->send();
        }
    }

    protected function getViewData(): array
    {
        return [
            'checkResult' => $this->checkResult,
        ];
    }
}
