<?php

declare(strict_types=1);

namespace App\Framework\Attributes;

use App\Framework\Http\Versioning\ApiVersion;
use Attribute;

/**
 * Attribute to specify API version requirements for controllers and methods
 */
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)]
final readonly class ApiVersionAttribute
{
    public ApiVersion $version;

    /**
     * @param string|ApiVersion $version API version (e.g., "1.0.0", "v2", or ApiVersion instance)
     * @param string|null $introducedIn Version when this endpoint was introduced
     * @param string|null $deprecatedIn Version when this endpoint was deprecated
     * @param string|null $removedIn Version when this endpoint will be removed
     */
    public function __construct(
        string|ApiVersion $version,
        public ?string $introducedIn = null,
        public ?string $deprecatedIn = null,
        public ?string $removedIn = null
    ) {
        $this->version = $version instanceof ApiVersion ? $version : ApiVersion::fromString($version);
    }

    public function isDeprecated(): bool
    {
        return $this->deprecatedIn !== null;
    }

    public function isRemoved(): bool
    {
        return $this->removedIn !== null;
    }

    public function getDeprecatedVersion(): ?ApiVersion
    {
        return $this->deprecatedIn ? ApiVersion::fromString($this->deprecatedIn) : null;
    }

    public function getRemovedVersion(): ?ApiVersion
    {
        return $this->removedIn ? ApiVersion::fromString($this->removedIn) : null;
    }

    public function getIntroducedVersion(): ?ApiVersion
    {
        return $this->introducedIn ? ApiVersion::fromString($this->introducedIn) : null;
    }
}
