<?php

declare(strict_types=1);

namespace App\Support\Governance;

use InvalidArgumentException;

final readonly class PlatformSubjectDescriptor
{
    public function __construct(
        public string $domainKey,
        public string $subjectClass,
        public string $subjectTypeKey,
        public string $subjectTypeLabel,
        public string $platformNoun,
        public string $displayLabel,
        public ?string $legacyPolicyType = null,
        public string $ownerLayer = PlatformVocabularyGlossary::OWNER_PLATFORM_CORE,
    ) {
        foreach ([
            'domain key' => $this->domainKey,
            'subject class' => $this->subjectClass,
            'subject type key' => $this->subjectTypeKey,
            'subject type label' => $this->subjectTypeLabel,
            'platform noun' => $this->platformNoun,
            'display label' => $this->displayLabel,
        ] as $label => $value) {
            if (trim($value) === '') {
                throw new InvalidArgumentException(sprintf('Platform subject descriptors require a non-empty %s.', $label));
            }
        }
    }

    /**
     * @return array{
     *     domain_key: string,
     *     subject_class: string,
     *     subject_type_key: string,
     *     subject_type_label: string,
     *     platform_noun: string,
     *     display_label: string,
     *     legacy_policy_type: ?string,
     *     owner_layer: string
     * }
     */
    public function toArray(): array
    {
        return [
            'domain_key' => $this->domainKey,
            'subject_class' => $this->subjectClass,
            'subject_type_key' => $this->subjectTypeKey,
            'subject_type_label' => $this->subjectTypeLabel,
            'platform_noun' => $this->platformNoun,
            'display_label' => $this->displayLabel,
            'legacy_policy_type' => $this->legacyPolicyType,
            'owner_layer' => $this->ownerLayer,
        ];
    }
}