<?php

declare(strict_types=1);

namespace App\Support;

use InvalidArgumentException;

final readonly class OperationTypeAlias
{
    public function __construct(
        public string $rawValue,
        public string $canonicalCode,
        public string $aliasStatus,
        public bool $writeAllowed,
        public ?string $deprecationNote = null,
        public ?string $retirementPath = null,
    ) {
        if (trim($this->rawValue) === '' || trim($this->canonicalCode) === '') {
            throw new InvalidArgumentException('Operation type aliases require a raw value and canonical code.');
        }
    }

    /**
     * @return array{
     *     raw_value: string,
     *     canonical_code: string,
     *     alias_status: string,
     *     write_allowed: bool,
     *     deprecation_note: ?string,
     *     retirement_path: ?string
     * }
     */
    public function toArray(): array
    {
        return [
            'raw_value' => $this->rawValue,
            'canonical_code' => $this->canonicalCode,
            'alias_status' => $this->aliasStatus,
            'write_allowed' => $this->writeAllowed,
            'deprecation_note' => $this->deprecationNote,
            'retirement_path' => $this->retirementPath,
        ];
    }

    public function canonicalName(): string
    {
        return $this->canonicalCode;
    }

    /**
     * @return array{
     *     raw_value: string,
     *     canonical_name: string,
     *     alias_status: string,
     *     write_allowed: bool,
     *     deprecation_note: ?string,
     *     retirement_path: ?string
     * }
     */
    public function retirementMetadata(): array
    {
        return [
            'raw_value' => $this->rawValue,
            'canonical_name' => $this->canonicalName(),
            'alias_status' => $this->aliasStatus,
            'write_allowed' => $this->writeAllowed,
            'deprecation_note' => $this->deprecationNote,
            'retirement_path' => $this->retirementPath,
        ];
    }
}