<?php

declare(strict_types=1);

namespace App\Support\OpsUx;

use InvalidArgumentException;

final readonly class GovernanceRunDiagnosticSummary
{
    /**
     * @param  array{label: string, value: string, source: string, confidence?: string}|null  $affectedScaleCue
     * @param  array{
     *     code: ?string,
     *     label: string,
     *     explanation: string
     * }  $dominantCause
     * @param  list<array{
     *     code: ?string,
     *     label: string,
     *     explanation: string
     * }>  $secondaryCauses
     * @param  list<array{
     *     label: string,
     *     value: string,
     *     hint?: ?string,
     *     emphasis?: string
     * }>  $secondaryFacts
     */
    public function __construct(
        public string $headline,
        public string $executionOutcomeLabel,
        public string $artifactImpactLabel,
        public string $primaryReason,
        public ?array $affectedScaleCue,
        public string $nextActionCategory,
        public string $nextActionText,
        public array $dominantCause,
        public array $secondaryCauses = [],
        public array $secondaryFacts = [],
        public bool $diagnosticsAvailable = false,
    ) {
        foreach ([
            'headline' => $this->headline,
            'executionOutcomeLabel' => $this->executionOutcomeLabel,
            'artifactImpactLabel' => $this->artifactImpactLabel,
            'primaryReason' => $this->primaryReason,
            'nextActionCategory' => $this->nextActionCategory,
            'nextActionText' => $this->nextActionText,
        ] as $field => $value) {
            if (trim($value) === '') {
                throw new InvalidArgumentException("Governance run summaries require {$field}.");
            }
        }

        if (trim((string) ($this->dominantCause['label'] ?? '')) === '' || trim((string) ($this->dominantCause['explanation'] ?? '')) === '') {
            throw new InvalidArgumentException('Governance run summaries require a dominant cause label and explanation.');
        }
    }

    /**
     * @return array{
     *     headline: string,
     *     executionOutcomeLabel: string,
     *     artifactImpactLabel: string,
     *     primaryReason: string,
     *     affectedScaleCue: array{label: string, value: string, source: string, confidence?: string}|null,
     *     nextActionCategory: string,
     *     nextActionText: string,
     *     dominantCause: array{code: ?string, label: string, explanation: string},
     *     secondaryCauses: list<array{code: ?string, label: string, explanation: string}>,
     *     secondaryFacts: list<array{label: string, value: string, hint?: ?string, emphasis?: string}>,
     *     diagnosticsAvailable: bool
     * }
     */
    public function toArray(): array
    {
        return [
            'headline' => $this->headline,
            'executionOutcomeLabel' => $this->executionOutcomeLabel,
            'artifactImpactLabel' => $this->artifactImpactLabel,
            'primaryReason' => $this->primaryReason,
            'affectedScaleCue' => $this->affectedScaleCue,
            'nextActionCategory' => $this->nextActionCategory,
            'nextActionText' => $this->nextActionText,
            'dominantCause' => $this->dominantCause,
            'secondaryCauses' => $this->secondaryCauses,
            'secondaryFacts' => $this->secondaryFacts,
            'diagnosticsAvailable' => $this->diagnosticsAvailable,
        ];
    }
}
