<?php

declare(strict_types=1);

namespace App\Framework\Notification\Channels\Telegram\ValueObjects;

/**
 * Telegram Bot Token Value Object
 *
 * Format: {bot_id}:{auth_token}
 * Example: 123456789:ABCdefGHIjklMNOpqrsTUVwxyz
 */
final readonly class TelegramBotToken
{
    public function __construct(
        public string $value
    ) {
        if (empty($value)) {
            throw new \InvalidArgumentException('Telegram bot token cannot be empty');
        }

        if (!$this->isValid($value)) {
            throw new \InvalidArgumentException(
                "Invalid Telegram bot token format: {$value}. Expected format: {bot_id}:{auth_token}"
            );
        }
    }

    public static function fromString(string $value): self
    {
        return new self($value);
    }

    private function isValid(string $token): bool
    {
        // Telegram bot token format: {bot_id}:{auth_token}
        // bot_id: numeric
        // auth_token: alphanumeric + dash + underscore
        return preg_match('/^\d+:[A-Za-z0-9_-]+$/', $token) === 1;
    }

    public function getBotId(): string
    {
        return explode(':', $this->value)[0];
    }

    public function getAuthToken(): string
    {
        return explode(':', $this->value)[1];
    }

    public function toString(): string
    {
        return $this->value;
    }

    public function __toString(): string
    {
        return $this->value;
    }

    public function equals(self $other): bool
    {
        return $this->value === $other->value;
    }
}
