<?php

namespace App\Filament\Widgets;

use App\Support\AdminDashboard;
use Filament\Widgets\ChartWidget;

class AttemptsChart extends ChartWidget
{
    protected ?string $heading = 'Journey activity, last 30 days';

    protected ?string $description = 'Member signups, completed checks, saved plans and checkout starts on one timeline.';

    protected int|string|array $columnSpan = ['md' => 2, 'xl' => 8];

    protected static ?int $sort = -3;

    protected function getData(): array
    {
        $trend = app(AdminDashboard::class)->snapshot()['activityTrend'];

        return [
            'datasets' => [
                ['label' => 'Members', 'data' => $trend['members'], 'borderColor' => '#1c2b5f', 'backgroundColor' => 'rgba(28,43,95,.08)', 'tension' => .35],
                ['label' => 'Assessments', 'data' => $trend['assessments'], 'borderColor' => '#58b9e6', 'backgroundColor' => 'rgba(88,185,230,.08)', 'tension' => .35],
                ['label' => 'Plans', 'data' => $trend['plans'], 'borderColor' => '#d97706', 'backgroundColor' => 'rgba(217,119,6,.06)', 'tension' => .35],
                ['label' => 'Checkouts', 'data' => $trend['checkouts'], 'borderColor' => '#16a34a', 'backgroundColor' => 'rgba(22,163,74,.06)', 'tension' => .35],
            ],
            'labels' => $trend['labels'],
        ];
    }

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

    protected function getOptions(): array
    {
        return [
            'interaction' => ['mode' => 'index', 'intersect' => false],
            'plugins' => ['legend' => ['position' => 'bottom']],
            'scales' => ['y' => ['beginAtZero' => true, 'ticks' => ['precision' => 0]]],
        ];
    }
}
