<?php

declare(strict_types=1);

namespace App\Support\Ui\OperatorExplanation;

use InvalidArgumentException;

final readonly class CountDescriptor
{
    public const string ROLE_EXECUTION = 'execution';

    public const string ROLE_EVALUATION_OUTPUT = 'evaluation_output';

    public const string ROLE_COVERAGE = 'coverage';

    public const string ROLE_RELIABILITY_SIGNAL = 'reliability_signal';

    public const string VISIBILITY_PRIMARY = 'primary';

    public const string VISIBILITY_DIAGNOSTIC = 'diagnostic';

    public function __construct(
        public string $label,
        public int $value,
        public string $role,
        public ?string $qualifier = null,
        public string $visibilityTier = self::VISIBILITY_PRIMARY,
    ) {
        if (trim($this->label) === '') {
            throw new InvalidArgumentException('Count descriptors require a label.');
        }

        if (! in_array($this->role, [
            self::ROLE_EXECUTION,
            self::ROLE_EVALUATION_OUTPUT,
            self::ROLE_COVERAGE,
            self::ROLE_RELIABILITY_SIGNAL,
        ], true)) {
            throw new InvalidArgumentException('Unsupported count descriptor role: '.$this->role);
        }

        if (! in_array($this->visibilityTier, [self::VISIBILITY_PRIMARY, self::VISIBILITY_DIAGNOSTIC], true)) {
            throw new InvalidArgumentException('Unsupported count descriptor visibility tier: '.$this->visibilityTier);
        }
    }

    /**
     * @return array{
     *     label: string,
     *     value: int,
     *     role: string,
     *     qualifier: ?string,
     *     visibilityTier: string
     * }
     */
    public function toArray(): array
    {
        return [
            'label' => $this->label,
            'value' => $this->value,
            'role' => $this->role,
            'qualifier' => $this->qualifier,
            'visibilityTier' => $this->visibilityTier,
        ];
    }
}
