<?php

declare(strict_types=1);

namespace App\Framework\MagicLinks;

use App\Framework\Http\ValueObjects\IpPatternCollection;
use App\Framework\MagicLinks\ValueObjects\Metadata;
use DateTimeImmutable;
use InvalidArgumentException;

final readonly class TokenConfig
{
    public function __construct(
        public int $expiryHours = 24,
        public bool $oneTimeUse = false,
        public int $maxUses = 1,
        public bool $requireSecureContext = false,
        public ?IpPatternCollection $allowedIpRanges = null,
        public ?Metadata $metadata = null
    ) {
        if ($expiryHours <= 0) {
            throw new InvalidArgumentException('Expiry hours must be positive');
        }

        if ($maxUses <= 0) {
            throw new InvalidArgumentException('Max uses must be positive');
        }
    }

    public static function default(): self
    {
        return new self(
            allowedIpRanges: IpPatternCollection::empty(),
            metadata: Metadata::empty()
        );
    }

    public function getAllowedIpRanges(): IpPatternCollection
    {
        return $this->allowedIpRanges ?? IpPatternCollection::empty();
    }

    public function getMetadata(): Metadata
    {
        return $this->metadata ?? Metadata::empty();
    }

    public function getExpiryDateTime(): DateTimeImmutable
    {
        return new DateTimeImmutable("+{$this->expiryHours} hours");
    }
}
