<?php

namespace App\Filament\Pages;

use App\Support\Analytics as AnalyticsEngine;
use BackedEnum;
use Carbon\CarbonImmutable;
use Filament\Pages\Page;

class Analytics extends Page
{
    protected string $view = 'filament.pages.analytics';

    protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-chart-bar';

    protected static ?string $navigationLabel = 'Analytics';

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

    protected static ?int $navigationSort = -1;

    protected static ?string $slug = 'analytics';

    protected static ?string $title = 'Platform analytics';

    public string $range = '30d';

    public ?string $from = null;

    public ?string $until = null;

    public ?string $country = null;

    public ?string $gender = null;

    public ?string $ageBand = null;

    public ?string $audience = null;

    public ?string $device = null;

    public function updatedRange(): void
    {
        if ($this->range !== 'custom') {
            $this->from = null;
            $this->until = null;
        }
    }

    public function resetFilters(): void
    {
        $this->reset(['range', 'from', 'until', 'country', 'gender', 'ageBand', 'audience', 'device']);
        $this->range = '30d';
    }

    private function engine(): AnalyticsEngine
    {
        [$from, $until] = match ($this->range) {
            'today' => [CarbonImmutable::today(), CarbonImmutable::now()],
            '7d' => [CarbonImmutable::today()->subDays(6), CarbonImmutable::now()],
            '90d' => [CarbonImmutable::today()->subDays(89), CarbonImmutable::now()],
            'custom' => [
                CarbonImmutable::parse($this->from ?: now()->subDays(29)->toDateString())->startOfDay(),
                CarbonImmutable::parse($this->until ?: now()->toDateString())->endOfDay(),
            ],
            default => [CarbonImmutable::today()->subDays(29), CarbonImmutable::now()],
        };

        return new AnalyticsEngine(
            from: $from,
            until: $until,
            country: $this->country ?: null,
            gender: $this->gender ?: null,
            ageBand: $this->ageBand ?: null,
            audience: $this->audience ?: null,
            device: $this->device ?: null,
        );
    }

    protected function getViewData(): array
    {
        $engine = $this->engine();

        return [
            'kpis' => $engine->kpis(),
            'traffic' => $engine->trafficByDay(),
            'topPages' => $engine->topPages(),
            'tools' => $engine->toolUsage(),
            'assessments' => $engine->assessmentFunnel(),
            'eventsBreakdown' => $engine->eventBreakdown(),
            'devices' => $engine->deviceSplit(),
            'genders' => $engine->genderSplit(),
            'ages' => $engine->ageBands(),
            'locations' => $engine->locations(),
            'goals' => $engine->goals(),
            'countryOptions' => $engine->countryOptions(),
            'rangeLabel' => $engine->from->format('j M').' – '.$engine->until->format('j M Y'),
        ];
    }
}
