<?php

declare(strict_types=1);

namespace App\Framework\OAuth\ValueObjects;

/**
 * Refresh Token Value Object
 *
 * Represents an OAuth refresh token with validation and masking
 */
final readonly class RefreshToken
{
    public function __construct(
        private string $token,
    ) {
        if (empty($token)) {
            throw new \InvalidArgumentException('Refresh token cannot be empty');
        }

        if (strlen($token) < 10) {
            throw new \InvalidArgumentException('Refresh token appears invalid (too short)');
        }
    }

    public static function create(string $token): self
    {
        return new self($token);
    }

    /**
     * Get the raw token value
     */
    public function toString(): string
    {
        return $this->token;
    }

    /**
     * Get masked token for logging (shows first/last 4 chars)
     */
    public function getMasked(): string
    {
        if (strlen($this->token) <= 12) {
            return str_repeat('*', strlen($this->token));
        }

        $first = substr($this->token, 0, 4);
        $last = substr($this->token, -4);
        $masked = str_repeat('*', strlen($this->token) - 8);

        return $first . $masked . $last;
    }

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