<?php

declare(strict_types=1);

namespace App\Application\LiveComponents\Services;

use App\Application\LiveComponents\ValueObjects\MetricColor;
use App\Application\LiveComponents\ValueObjects\MetricFormat;
use App\Application\LiveComponents\ValueObjects\MetricId;
use App\Application\LiveComponents\ValueObjects\MetricValue;
use App\Application\LiveComponents\ValueObjects\TimeRange;
use App\Framework\LiveComponents\Attributes\DataProvider;

/**
 * DemoMetricsDataProvider - Provides demo/sample metrics data
 *
 * Used for development, testing, and demos
 */
#[DataProvider(name: 'demo')]
final readonly class DemoMetricsDataProvider implements MetricsDataProvider
{
    public function getMetricsForTimeRange(TimeRange $timeRange): array
    {
        return $this->generateMetrics($timeRange);
    }

    public function refreshMetrics(TimeRange $timeRange): array
    {
        // For demo, just regenerate with new random values
        return $this->generateMetrics($timeRange);
    }

    /**
     * @return array<MetricValue>
     */
    private function generateMetrics(TimeRange $timeRange): array
    {
        $multiplier = $timeRange->getMultiplier();

        // Base values that scale with time range
        $baseRevenue = 45000;
        $baseUsers = 12500;
        $baseConversions = 850;
        $baseOrders = 320;

        return [
            new MetricValue(
                id: MetricId::revenue(),
                label: 'Umsatz',
                value: $baseRevenue * $multiplier + rand(-5000, 5000),
                previousValue: $baseRevenue * $multiplier * 0.9 + rand(-3000, 3000),
                format: MetricFormat::CURRENCY,
                icon: '💰',
                color: MetricColor::BLUE,
                goal: $baseRevenue * $multiplier * 1.1,
                suffix: '€'
            ),
            new MetricValue(
                id: MetricId::users(),
                label: 'Aktive Nutzer',
                value: $baseUsers * $multiplier + rand(-1000, 1000),
                previousValue: $baseUsers * $multiplier * 0.95 + rand(-500, 500),
                format: MetricFormat::NUMBER,
                icon: '👥',
                color: MetricColor::GREEN,
                goal: $baseUsers * $multiplier * 1.2
            ),
            new MetricValue(
                id: MetricId::conversions(),
                label: 'Conversions',
                value: $baseConversions * $multiplier + rand(-50, 50),
                previousValue: $baseConversions * $multiplier * 0.92 + rand(-30, 30),
                format: MetricFormat::NUMBER,
                icon: '🎯',
                color: MetricColor::PURPLE,
                goal: $baseConversions * $multiplier * 1.15
            ),
            new MetricValue(
                id: MetricId::orders(),
                label: 'Bestellungen',
                value: $baseOrders * $multiplier + rand(-20, 20),
                previousValue: $baseOrders * $multiplier * 1.05 + rand(-15, 15),
                format: MetricFormat::NUMBER,
                icon: '🛒',
                color: MetricColor::ORANGE,
                goal: $baseOrders * $multiplier * 1.1
            ),
            new MetricValue(
                id: MetricId::averageOrderValue(),
                label: 'Ø Bestellwert',
                value: 140 + rand(-10, 10),
                previousValue: 135 + rand(-5, 5),
                format: MetricFormat::CURRENCY,
                icon: '💳',
                color: MetricColor::CYAN,
                goal: 150,
                suffix: '€'
            ),
            new MetricValue(
                id: MetricId::bounceRate(),
                label: 'Absprungrate',
                value: 42 + rand(-5, 5),
                previousValue: 45 + rand(-3, 3),
                format: MetricFormat::PERCENTAGE,
                icon: '📊',
                color: MetricColor::RED,
                goal: 35,
                suffix: '%',
                inverted: true // Lower is better
            ),
        ];
    }
}
