<?php

declare(strict_types=1);

namespace App\Application\Analytics\ValueObject;

use App\Framework\Core\ValueObjects\Percentage;

/**
 * Country usage breakdown for analytics reporting
 */
final readonly class CountryBreakdown
{
    public function __construct(
        public int $germany,
        public int $austria,
        public int $switzerland,
        public int $others,
    ) {
        if ($germany < 0 || $austria < 0 || $switzerland < 0 || $others < 0) {
            throw new \InvalidArgumentException('Country counts cannot be negative');
        }
    }

    /**
     * Create from legacy array format: ['Germany' => 70, 'Austria' => 15, ...]
     * @param array<string, int> $data
     */
    public static function fromArray(array $data): self
    {
        return new self(
            germany: $data['Germany'] ?? 0,
            austria: $data['Austria'] ?? 0,
            switzerland: $data['Switzerland'] ?? 0,
            others: $data['Others'] ?? 0,
        );
    }

    public function getTotal(): int
    {
        return $this->germany + $this->austria + $this->switzerland + $this->others;
    }

    public function getGermanyPercentage(): Percentage
    {
        return Percentage::fromRatio($this->germany, $this->getTotal());
    }

    public function getAustriaPercentage(): Percentage
    {
        return Percentage::fromRatio($this->austria, $this->getTotal());
    }

    public function getSwitzerlandPercentage(): Percentage
    {
        return Percentage::fromRatio($this->switzerland, $this->getTotal());
    }

    public function getOthersPercentage(): Percentage
    {
        return Percentage::fromRatio($this->others, $this->getTotal());
    }

    public function getDachRegionTotal(): int
    {
        return $this->germany + $this->austria + $this->switzerland;
    }

    public function getDachRegionPercentage(): Percentage
    {
        return Percentage::fromRatio($this->getDachRegionTotal(), $this->getTotal());
    }

    /**
     * @return array<string, int>
     */
    public function toArray(): array
    {
        return [
            'Germany' => $this->germany,
            'Austria' => $this->austria,
            'Switzerland' => $this->switzerland,
            'Others' => $this->others,
        ];
    }

    /**
     * Legacy format for backward compatibility
     * @return array<string, mixed>
     */
    public function toAnalyticsArray(): array
    {
        return [
            'Germany' => $this->germany,
            'Austria' => $this->austria,
            'Switzerland' => $this->switzerland,
            'Others' => $this->others,
            'germany_percentage' => $this->getGermanyPercentage()->format(),
            'austria_percentage' => $this->getAustriaPercentage()->format(),
            'switzerland_percentage' => $this->getSwitzerlandPercentage()->format(),
            'others_percentage' => $this->getOthersPercentage()->format(),
            'dach_total' => $this->getDachRegionTotal(),
            'dach_percentage' => $this->getDachRegionPercentage()->format(),
            'total' => $this->getTotal(),
        ];
    }
}
