<?php

namespace App\Filament\RealEstate\Pages;

use App\Enums\LeadStatus;
use App\Enums\PropertyStatus;
use App\Filament\RealEstate\Widgets\RealEstateLeadPipelineChart;
use App\Filament\RealEstate\Widgets\RealEstateLeadTrendChart;
use App\Filament\RealEstate\Widgets\RealEstateOverdueInstallmentsTable;
use App\Filament\RealEstate\Widgets\RealEstatePropertyStatusChart;
use App\Filament\RealEstate\Widgets\RealEstateRecentBookingsTable;
use App\Filament\RealEstate\Widgets\RealEstateRecentLeadsTable;
use App\Filament\RealEstate\Widgets\RealEstateRevenueTrendChart;
use App\Filament\RealEstate\Widgets\RealEstateStatsWidget;
use App\Models\Project;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Pages\Dashboard as BaseDashboard;
use Filament\Pages\Dashboard\Concerns\HasFiltersForm;
use Illuminate\Contracts\Support\Htmlable;

class Dashboard extends BaseDashboard
{
    use HasFiltersForm;

    protected static ?string $navigationIcon = 'heroicon-o-chart-bar-square';

    protected static ?string $title = 'Analytics Dashboard';

    public function mount(): void
    {
        $this->mountHasFilters();

        if (empty($this->filters['period'] ?? null)) {
            $this->filters = [
                'period' => '180',
                'project_id' => null,
                'property_type' => null,
                'property_status' => null,
                'lead_status' => null,
                'startDate' => null,
                'endDate' => null,
            ];
            $this->getFiltersForm()->fill($this->filters);
        }
    }

    public function getHeading(): string | Htmlable
    {
        return 'Real Estate Analytics';
    }

    public function getSubheading(): string | Htmlable | null
    {
        return 'Live dashboard — filters update all charts & tables automatically every 30 seconds';
    }

    public function filtersForm(Form $form): Form
    {
        return $form->schema([
            Section::make('Filters')
                ->description('Change any filter — all widgets refresh instantly')
                ->schema([
                    Select::make('period')
                        ->label('Time period')
                        ->options([
                            '7' => 'Last 7 days',
                            '30' => 'Last 30 days',
                            '90' => 'Last 3 months',
                            '180' => 'Last 6 months',
                            '365' => 'Last 12 months',
                            'custom' => 'Custom range',
                        ])
                        ->default('180')
                        ->live()
                        ->native(false),
                    Select::make('project_id')
                        ->label('Project / Society')
                        ->options(fn () => Project::query()->orderBy('name')->pluck('name', 'id'))
                        ->searchable()
                        ->placeholder('All projects')
                        ->live()
                        ->native(false),
                    Select::make('property_type')
                        ->label('Property type')
                        ->options([
                            'plot' => 'Plot',
                            'apartment' => 'Apartment',
                            'commercial' => 'Commercial',
                            'house' => 'House',
                        ])
                        ->placeholder('All types')
                        ->live()
                        ->native(false),
                    Select::make('property_status')
                        ->label('Property status')
                        ->options(collect(PropertyStatus::cases())->mapWithKeys(
                            fn (PropertyStatus $s) => [$s->value => $s->label()]
                        ))
                        ->placeholder('All statuses')
                        ->live()
                        ->native(false),
                    Select::make('lead_status')
                        ->label('Lead status')
                        ->options(collect(LeadStatus::cases())->mapWithKeys(
                            fn (LeadStatus $s) => [$s->value => $s->label()]
                        ))
                        ->placeholder('All leads')
                        ->live()
                        ->native(false),
                    DatePicker::make('startDate')
                        ->label('From')
                        ->visible(fn (Get $get): bool => $get('period') === 'custom')
                        ->live(),
                    DatePicker::make('endDate')
                        ->label('To')
                        ->visible(fn (Get $get): bool => $get('period') === 'custom')
                        ->live(),
                ])
                ->columns(['default' => 1, 'md' => 2, 'xl' => 4])
                ->columnSpanFull(),
        ]);
    }

    /**
     * @return array<class-string>
     */
    public function getWidgets(): array
    {
        return [
            RealEstateStatsWidget::class,
            RealEstateLeadPipelineChart::class,
            RealEstatePropertyStatusChart::class,
            RealEstateRevenueTrendChart::class,
            RealEstateLeadTrendChart::class,
            RealEstateRecentLeadsTable::class,
            RealEstateOverdueInstallmentsTable::class,
            RealEstateRecentBookingsTable::class,
        ];
    }

    public function getColumns(): int | string | array
    {
        return 2;
    }
}
