<?php

declare(strict_types=1);

namespace App\Framework\Auth\ValueObjects;

/**
 * Value Object für IP-Authentifizierungs-Entscheidungen
 */
final readonly class IpAuthDecision
{
    private function __construct(
        public bool $allowed,
        public string $reason,
        public ?string $matchedPattern = null
    ) {
    }

    /**
     * Create allowed decision
     */
    public static function allowed(string $reason, ?string $matchedPattern = null): self
    {
        return new self(true, $reason, $matchedPattern);
    }

    /**
     * Create denied decision
     */
    public static function denied(string $reason, ?string $matchedPattern = null): self
    {
        return new self(false, $reason, $matchedPattern);
    }

    /**
     * Check if access is allowed
     */
    public function isAllowed(): bool
    {
        return $this->allowed;
    }

    /**
     * Check if access is denied
     */
    public function isDenied(): bool
    {
        return ! $this->allowed;
    }

    /**
     * Get human-readable decision
     */
    public function getDecision(): string
    {
        return $this->allowed ? 'ALLOWED' : 'DENIED';
    }

    /**
     * Convert to array
     * @return array<string, mixed>
     */
    public function toArray(): array
    {
        return [
            'allowed' => $this->allowed,
            'decision' => $this->getDecision(),
            'reason' => $this->reason,
            'matched_pattern' => $this->matchedPattern,
        ];
    }

    /**
     * Convert to string representation
     */
    public function toString(): string
    {
        $decision = $this->getDecision();
        $pattern = $this->matchedPattern ? " (Pattern: {$this->matchedPattern})" : '';

        return "{$decision}: {$this->reason}{$pattern}";
    }

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