<?php

declare(strict_types=1);

namespace App\Support\Baselines;

use InvalidArgumentException;

final readonly class TenantGovernanceAggregate
{
    public function __construct(
        public int $tenantId,
        public int $workspaceId,
        public ?string $profileName,
        public string $compareState,
        public string $stateFamily,
        public string $tone,
        public string $headline,
        public ?string $supportingMessage,
        public ?string $reasonCode,
        public ?string $lastComparedLabel,
        public int $visibleDriftFindingsCount,
        public int $overdueOpenFindingsCount,
        public int $expiringGovernanceCount,
        public int $lapsedGovernanceCount,
        public int $activeNonNewFindingsCount,
        public int $highSeverityActiveFindingsCount,
        public string $nextActionLabel,
        public string $nextActionTarget,
        public bool $positiveClaimAllowed,
        public BaselineCompareStats $stats,
        public BaselineCompareSummaryAssessment $summaryAssessment,
    ) {
        if ($this->tenantId <= 0) {
            throw new InvalidArgumentException('ManagedEnvironment governance aggregates require a positive tenant id.');
        }

        if ($this->workspaceId <= 0) {
            throw new InvalidArgumentException('ManagedEnvironment governance aggregates require a positive workspace id.');
        }

        if (trim($this->compareState) === '') {
            throw new InvalidArgumentException('ManagedEnvironment governance aggregates require a compare state.');
        }

        if (trim($this->headline) === '') {
            throw new InvalidArgumentException('ManagedEnvironment governance aggregates require a headline.');
        }

        if (trim($this->nextActionLabel) === '') {
            throw new InvalidArgumentException('ManagedEnvironment governance aggregates require a next-action label.');
        }

        if (! in_array($this->nextActionTarget, [
            BaselineCompareSummaryAssessment::NEXT_TARGET_FINDINGS,
            BaselineCompareSummaryAssessment::NEXT_TARGET_LANDING,
            BaselineCompareSummaryAssessment::NEXT_TARGET_RUN,
            BaselineCompareSummaryAssessment::NEXT_TARGET_NONE,
        ], true)) {
            throw new InvalidArgumentException('ManagedEnvironment governance aggregates require a supported next-action target.');
        }
    }

    /**
     * @return array{
     *     tenantId: int,
     *     workspaceId: int,
     *     profileName: ?string,
     *     compareState: string,
     *     stateFamily: string,
     *     tone: string,
     *     headline: string,
     *     supportingMessage: ?string,
     *     reasonCode: ?string,
     *     lastComparedLabel: ?string,
     *     visibleDriftFindingsCount: int,
     *     overdueOpenFindingsCount: int,
     *     expiringGovernanceCount: int,
     *     lapsedGovernanceCount: int,
     *     activeNonNewFindingsCount: int,
     *     highSeverityActiveFindingsCount: int,
     *     nextActionLabel: string,
     *     nextActionTarget: string,
     *     positiveClaimAllowed: bool
     * }
     */
    public function toArray(): array
    {
        return [
            'tenantId' => $this->tenantId,
            'workspaceId' => $this->workspaceId,
            'profileName' => $this->profileName,
            'compareState' => $this->compareState,
            'stateFamily' => $this->stateFamily,
            'tone' => $this->tone,
            'headline' => $this->headline,
            'supportingMessage' => $this->supportingMessage,
            'reasonCode' => $this->reasonCode,
            'lastComparedLabel' => $this->lastComparedLabel,
            'visibleDriftFindingsCount' => $this->visibleDriftFindingsCount,
            'overdueOpenFindingsCount' => $this->overdueOpenFindingsCount,
            'expiringGovernanceCount' => $this->expiringGovernanceCount,
            'lapsedGovernanceCount' => $this->lapsedGovernanceCount,
            'activeNonNewFindingsCount' => $this->activeNonNewFindingsCount,
            'highSeverityActiveFindingsCount' => $this->highSeverityActiveFindingsCount,
            'nextActionLabel' => $this->nextActionLabel,
            'nextActionTarget' => $this->nextActionTarget,
            'positiveClaimAllowed' => $this->positiveClaimAllowed,
        ];
    }
}
