<?php

namespace App\Filament\Pages\Accounting;

use App\Services\Accounting\Reports\BalanceSheetReport;
use Filament\Forms;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Pages\Page;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use BezhanSalleh\FilamentShield\Traits\HasPageShield;
use Filament\Support\Icons\Heroicon;

class BalanceSheet extends Page implements HasForms
{
    use InteractsWithForms;
    use HasPageShield;

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

    protected string $view = 'filament.pages.accounting.balance-sheet';

    protected static ?int $navigationSort = 12;

    public ?array $data = [];
    public ?array $reportData = null;

    public static function getNavigationGroup(): ?string
    {
        return __('models.Accounting');
    }

    public static function getNavigationLabel(): string
    {
        return __('Balance Sheet');
    }

    public function getTitle(): string
    {
        return __('Balance Sheet Report');
    }

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

    public function form(Schema $schema): Schema
    {
        return $schema
            ->schema([
                Section::make(__('Report Parameters'))
                    ->schema([
                        Forms\Components\DatePicker::make('as_of_date')
                            ->label(__('As of Date'))
                            ->required()
                            ->native(false)
                            ->default(now()),
                    ]),
            ])
            ->statePath('data');
    }

    public function generateReport(): void
    {
        $data = $this->form->getState();

        try {
            $reportService = new BalanceSheetReport();
            $this->reportData = $reportService->generate($data['as_of_date']);

            \Filament\Notifications\Notification::make()
                ->success()
                ->title(__('Report Generated'))
                ->body(__('Balance sheet has been generated successfully.'))
                ->send();
        } catch (\Exception $e) {
            \Filament\Notifications\Notification::make()
                ->danger()
                ->title(__('Error'))
                ->body($e->getMessage())
                ->send();
        }
    }

    public function exportPdf()
    {
        $data = $this->form->getState();

        try {
            $reportService = new BalanceSheetReport();
            $pdf = $reportService->exportPdf($data['as_of_date']);

            return response()->streamDownload(function () use ($pdf) {
                echo $pdf;
            }, 'balance-sheet-' . now()->format('Y-m-d') . '.pdf');
        } catch (\Exception $e) {
            \Filament\Notifications\Notification::make()
                ->danger()
                ->title(__('Error'))
                ->body($e->getMessage())
                ->send();
        }
    }

    protected function getFormActions(): array
    {
        return [
            \Filament\Actions\Action::make('generate')
                ->label(__('filament/admin/balance_sheet.generate'))
                ->action('generateReport')
                ->color('primary'),

            \Filament\Actions\Action::make('export_pdf')
                ->label(__('filament/admin/balance_sheet.export_pdf'))
                ->action('exportPdf')
                ->color('success')
                ->visible(fn () => $this->reportData !== null),
        ];
    }
}
