<?php

declare(strict_types=1);

namespace App\Application\Analytics\ValueObject;

/**
 * User behavior analytics report containing all user interaction breakdowns
 */
final readonly class UserBehaviorReport
{
    public function __construct(
        public ActionBreakdown $actions,
        public DeviceBreakdown $devices,
        public BrowserBreakdown $browsers,
        public CountryBreakdown $countries,
    ) {
    }

    public function getTotalInteractions(): int
    {
        return $this->actions->getTotal();
    }

    public function getTotalUniqueUsers(): int
    {
        // In a real implementation, this would be calculated differently
        // For now, we'll estimate based on device usage
        return $this->devices->getTotal();
    }

    /**
     * @return array{action: string, count: int}|null
     */
    public function getMostPopularAction(): ?array
    {
        return $this->actions->getTopAction();
    }

    public function getDominantDevice(): string
    {
        $devices = $this->devices;
        $max = max($devices->desktop, $devices->mobile, $devices->tablet);

        return match ($max) {
            $devices->desktop => 'Desktop',
            $devices->mobile => 'Mobile',
            $devices->tablet => 'Tablet',
            default => 'Unknown'
        };
    }

    public function getDominantBrowser(): string
    {
        $browsers = $this->browsers;
        $max = max($browsers->chrome, $browsers->firefox, $browsers->safari, $browsers->edge);

        return match ($max) {
            $browsers->chrome => 'Chrome',
            $browsers->firefox => 'Firefox',
            $browsers->safari => 'Safari',
            $browsers->edge => 'Edge',
            default => 'Unknown'
        };
    }

    public function isDachRegionDominant(): bool
    {
        return $this->countries->getDachRegionPercentage()->getValue() > 80.0;
    }

    /**
     * Get insights about user behavior patterns
     * @return array<string, mixed>
     */
    public function getInsights(): array
    {
        return [
            'total_interactions' => $this->getTotalInteractions(),
            'estimated_unique_users' => $this->getTotalUniqueUsers(),
            'most_popular_action' => $this->getMostPopularAction(),
            'dominant_device' => $this->getDominantDevice(),
            'dominant_browser' => $this->getDominantBrowser(),
            'dach_region_focus' => $this->isDachRegionDominant(),
            'mobile_first_audience' => $this->devices->getMobilePercentage()->getValue() > 50.0,
            'chrome_dominance' => $this->browsers->getChromePercentage()->getValue() > 60.0,
        ];
    }

    /**
     * Legacy array format for backward compatibility
     * @return array<string, mixed>
     */
    public function toArray(): array
    {
        return [
            'actions' => $this->actions->toArray(),
            'devices' => $this->devices->toArray(),
            'browsers' => $this->browsers->toArray(),
            'countries' => $this->countries->toArray(),
        ];
    }

    /**
     * Enhanced analytics array with percentages and insights
     * @return array<string, mixed>
     */
    public function toAnalyticsArray(): array
    {
        return [
            'actions' => $this->actions->toAnalyticsArray(),
            'devices' => $this->devices->toAnalyticsArray(),
            'browsers' => $this->browsers->toAnalyticsArray(),
            'countries' => $this->countries->toAnalyticsArray(),
            'insights' => $this->getInsights(),
        ];
    }
}
