<?php

namespace App\Filament\Pages;

use App\Enums\RoleEnum;
use App\Filament\Widgets\Charts\BudgetRealizationChart;
use App\Filament\Widgets\Charts\CompanyStatusChart;
use App\Filament\Widgets\Charts\CooperationStatusChart;
use App\Filament\Widgets\Charts\IssueDepartmentChart;
use App\Filament\Widgets\Charts\IssueLocationChart;
use App\Filament\Widgets\Charts\IssueSentimentByDepartmentChart;
use App\Filament\Widgets\Charts\JournalistRegistrationChart;
use App\Filament\Widgets\Charts\MediaClassificationChart;
use App\Filament\Widgets\Charts\MediaCooperationRankingChart;
use App\Filament\Widgets\Charts\MediaRegistrationChart;
use App\Filament\Widgets\Charts\MediaTypeChart;
use App\Filament\Widgets\Charts\PopularNewsChart;
use App\Filament\Widgets\Charts\SentimentTrendChart;
use App\Filament\Widgets\Charts\TaskRealizationChart;
use App\Filament\Widgets\Charts\TopMediaJournalistChart;
use App\Filament\Widgets\Charts\VerificationPerformanceChart;
use App\Filament\Widgets\Charts\VisitorTrafficChart;
use BackedEnum;
use BezhanSalleh\FilamentShield\Traits\HasPageShield;
use Filament\Actions\Action;
use Filament\Forms\Components\DatePicker;
use Filament\Pages\Dashboard as BaseDashboard;
use Filament\Pages\Dashboard\Concerns\HasFiltersForm;
use Filament\Schemas\Components\Fieldset;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;

class Analytic extends BaseDashboard
{
    use HasFiltersForm, HasPageShield;

    protected static string $routePath = '/analytic';

    protected static ?string $slug = 'analytic';

    protected static ?string $title = 'Analisa';

    protected static ?string $navigationLabel = 'Analisa';

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

    protected static ?int $navigationSort = 2;

    protected static ?string $pollingInterval = null;

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

    public function filtersForm(Schema $schema): Schema
    {
        return $schema
            ->schema([
                Fieldset::make('Filter')
                    ->schema([
                        DatePicker::make('startDate')
                            ->label('Tanggal Mulai')
                            ->placeholder('Pilih tanggal mulai')
                            ->native(false)
                            ->displayFormat('l, d F Y'),

                        DatePicker::make('endDate')
                            ->label('Tanggal Selesai')
                            ->placeholder('Pilih tanggal selesai')
                            ->native(false)
                            ->displayFormat('l, d F Y'),
                    ])
                    ->columns(2)
                    ->columnSpanFull()
                    ->visible(fn () => auth()->user()->hasRole(RoleEnum::DEVELOPER->value) || auth()->user()->hasRole(RoleEnum::ADMINISTRATOR->value)),
            ]);
    }

    protected function getHeaderActions(): array
    {
        return [
            Action::make('reset')
                ->label('Reset Filter')
                ->icon(Heroicon::OutlinedXMark)
                ->color('danger')
                ->action(fn () => $this->resetFilters())
                ->visible(fn () => ! empty($this->filters['startDate']) || ! empty($this->filters['endDate'])),
        ];
    }

    public function resetFilters(): void
    {
        $this->filters['startDate'] = null;
        $this->filters['endDate'] = null;

        if (method_exists($this, 'getFiltersSessionKey')) {
            session()->forget($this->getFiltersSessionKey());
        }
    }

    public function getWidgets(): array
    {
        return [
            CompanyStatusChart::class,
            CooperationStatusChart::class,
            IssueDepartmentChart::class,
            IssueSentimentByDepartmentChart::class,
            IssueLocationChart::class,
            JournalistRegistrationChart::class,
            MediaClassificationChart::class,
            MediaCooperationRankingChart::class,
            MediaRegistrationChart::class,
            MediaTypeChart::class,
            PopularNewsChart::class,
            TaskRealizationChart::class,
            TopMediaJournalistChart::class,
            VerificationPerformanceChart::class,
            BudgetRealizationChart::class,
            SentimentTrendChart::class,
            VisitorTrafficChart::class,
        ];
    }
}
