<?php

declare(strict_types=1);

namespace App\Framework\Design\Analyzer;

use App\Framework\Design\ValueObjects\ComponentPattern;

/**
 * Ergebnis der Component-Pattern-Erkennung
 */
final readonly class ComponentDetectionResult
{
    /**
     * @param ComponentPattern[] $bemComponents
     * @param ComponentPattern[] $utilityComponents
     * @param ComponentPattern[] $traditionalComponents
     */
    public function __construct(
        public int $totalComponents,
        public array $bemComponents,
        public array $utilityComponents,
        public array $traditionalComponents,
        public array $patternStatistics,
        public array $recommendations
    ) {
    }

    /**
     * Gibt die dominante Pattern-Methodik zurück
     */
    public function getDominantPattern(): string
    {
        $counts = [
            'bem' => count($this->bemComponents),
            'utility' => count($this->utilityComponents),
            'traditional' => count($this->traditionalComponents),
        ];

        $max = max($counts);
        if ($max === 0) {
            return 'none';
        }

        return array_search($max, $counts);
    }

    /**
     * Gibt Pattern-Diversity-Score zurück (0-100)
     */
    public function getPatternDiversity(): int
    {
        $patterns = [$this->bemComponents, $this->utilityComponents, $this->traditionalComponents];
        $nonEmptyPatterns = array_filter($patterns, fn ($p) => ! empty($p));

        if (empty($nonEmptyPatterns)) {
            return 0;
        }

        // Je mehr verschiedene Pattern-Typen verwendet werden, desto höher die Diversity
        $diversityScore = (count($nonEmptyPatterns) / 3) * 100;

        return (int) round($diversityScore);
    }

    /**
     * Gibt Konsistenz-Score zurück (0-100)
     */
    public function getConsistencyScore(): int
    {
        $totalComponents = $this->totalComponents;

        if ($totalComponents === 0) {
            return 100;
        }

        $dominantCount = max(
            count($this->bemComponents),
            count($this->utilityComponents),
            count($this->traditionalComponents)
        );

        // Je höher der Anteil der dominanten Methodik, desto konsistenter
        $consistencyScore = ($dominantCount / $totalComponents) * 100;

        return (int) round($consistencyScore);
    }

    /**
     * Gibt empfohlene Aktionen basierend auf der Analyse zurück
     */
    public function getActionableRecommendations(): array
    {
        $actions = [];

        $consistency = $this->getConsistencyScore();
        $diversity = $this->getPatternDiversity();

        if ($consistency < 60 && $diversity > 80) {
            $actions[] = [
                'priority' => 'high',
                'action' => 'standardize_methodology',
                'description' => 'Standardize on one primary CSS methodology to improve consistency.',
            ];
        }

        if (count($this->bemComponents) > 5 && count($this->utilityComponents) < 3) {
            $actions[] = [
                'priority' => 'medium',
                'action' => 'add_utilities',
                'description' => 'Add utility classes for common spacing, colors, and typography.',
            ];
        }

        if (count($this->traditionalComponents) > 10) {
            $actions[] = [
                'priority' => 'medium',
                'action' => 'refactor_to_bem',
                'description' => 'Consider refactoring traditional components to BEM methodology.',
            ];
        }

        return $actions;
    }

    /**
     * Gibt Pattern-Verteilung zurück
     */
    public function getPatternDistribution(): array
    {
        $total = $this->totalComponents;

        if ($total === 0) {
            return [
                'bem' => 0,
                'utility' => 0,
                'traditional' => 0,
            ];
        }

        return [
            'bem' => round((count($this->bemComponents) / $total) * 100, 1),
            'utility' => round((count($this->utilityComponents) / $total) * 100, 1),
            'traditional' => round((count($this->traditionalComponents) / $total) * 100, 1),
        ];
    }

    /**
     * Konvertiert zu Array für Export
     */
    public function toArray(): array
    {
        return [
            'total_components' => $this->totalComponents,
            'bem_components' => array_map(fn (ComponentPattern $p) => $p->toArray(), $this->bemComponents),
            'utility_components' => array_map(fn (ComponentPattern $p) => $p->toArray(), $this->utilityComponents),
            'traditional_components' => array_map(fn (ComponentPattern $p) => $p->toArray(), $this->traditionalComponents),
            'pattern_statistics' => $this->patternStatistics,
            'recommendations' => $this->recommendations,
            'dominant_pattern' => $this->getDominantPattern(),
            'pattern_diversity' => $this->getPatternDiversity(),
            'consistency_score' => $this->getConsistencyScore(),
            'actionable_recommendations' => $this->getActionableRecommendations(),
        ];
    }
}
