<?php

namespace App\Filament\Widgets;

use App\Models\Application;
use App\Models\Payment;
use App\Models\ResumeReport;
use App\Models\User;
use Carbon\CarbonImmutable;
use Filament\Support\RawJs;
use Filament\Widgets\ChartWidget;

class PlatformActivityTrend extends ChartWidget
{
    protected static bool $isLazy = false;

    protected static ?int $sort = 2;

    protected ?string $heading = 'Growth and activation trend';

    protected ?string $description = 'Chart.js line chart for signups, resume reports, payments, and applications.';

    protected string $color = 'primary';

    protected string|int|array $columnSpan = 'full';

    public ?string $filter = '30';

    protected function getType(): string
    {
        return 'line';
    }

    /**
     * @return array<string, string>
     */
    protected function getFilters(): ?array
    {
        return [
            '14' => 'Last 14 days',
            '30' => 'Last 30 days',
            '90' => 'Last 90 days',
        ];
    }

    /**
     * @return array<string, mixed>
     */
    protected function getData(): array
    {
        $days = max(14, (int) ($this->filter ?: 30));
        $start = CarbonImmutable::now()->subDays($days - 1)->startOfDay();
        $dates = collect(range(0, $days - 1))
            ->map(fn (int $offset): CarbonImmutable => $start->addDays($offset));

        return [
            'datasets' => [
                [
                    'label' => 'New users',
                    'data' => $dates
                        ->map(fn (CarbonImmutable $date): int => User::query()
                            ->whereDate('created_at', $date)
                            ->count())
                        ->all(),
                    'borderColor' => '#0f766e',
                    'backgroundColor' => 'rgba(15, 118, 110, 0.13)',
                    'fill' => true,
                    'tension' => 0.42,
                    'pointRadius' => 0,
                ],
                [
                    'label' => 'Resume analyses',
                    'data' => $dates
                        ->map(fn (CarbonImmutable $date): int => ResumeReport::query()
                            ->where('status', ResumeReport::STATUS_COMPLETED)
                            ->whereDate('analyzed_at', $date)
                            ->count())
                        ->all(),
                    'borderColor' => '#2563eb',
                    'backgroundColor' => 'rgba(37, 99, 235, 0.08)',
                    'fill' => true,
                    'tension' => 0.42,
                    'pointRadius' => 0,
                ],
                [
                    'label' => 'Applications',
                    'data' => $dates
                        ->map(fn (CarbonImmutable $date): int => Application::query()
                            ->whereDate('applied_at', $date)
                            ->count())
                        ->all(),
                    'borderColor' => '#f59e0b',
                    'backgroundColor' => 'rgba(245, 158, 11, 0.10)',
                    'fill' => true,
                    'tension' => 0.42,
                    'pointRadius' => 0,
                ],
                [
                    'label' => 'Verified payments',
                    'data' => $dates
                        ->map(fn (CarbonImmutable $date): int => Payment::query()
                            ->whereDate('verified_at', $date)
                            ->count())
                        ->all(),
                    'borderColor' => '#10b981',
                    'backgroundColor' => 'rgba(16, 185, 129, 0.08)',
                    'fill' => true,
                    'tension' => 0.42,
                    'pointRadius' => 0,
                ],
            ],
            'labels' => $dates
                ->map(fn (CarbonImmutable $date): string => $date->format($days > 30 ? 'M j' : 'j M'))
                ->all(),
        ];
    }

    /**
     * @return array<string, mixed>|RawJs|null
     */
    protected function getOptions(): array|RawJs|null
    {
        return [
            'plugins' => [
                'legend' => [
                    'position' => 'bottom',
                    'labels' => [
                        'usePointStyle' => true,
                        'pointStyle' => 'circle',
                    ],
                ],
                'tooltip' => [
                    'mode' => 'index',
                    'intersect' => false,
                ],
            ],
            'interaction' => [
                'mode' => 'nearest',
                'axis' => 'x',
                'intersect' => false,
            ],
            'scales' => [
                'x' => [
                    'grid' => [
                        'display' => false,
                    ],
                ],
                'y' => [
                    'beginAtZero' => true,
                    'ticks' => [
                        'precision' => 0,
                    ],
                ],
            ],
        ];
    }
}
