<?php

namespace App\Services\Providers;

use App\Models\ProviderConnection;
use App\Support\Providers\ProviderReasonCodes;

final class ProviderConnectionResolution
{
    private function __construct(
        public readonly bool $resolved,
        public readonly ?ProviderConnection $connection,
        public readonly ?string $reasonCode,
        public readonly ?string $extensionReasonCode,
        public readonly ?string $message,
    ) {}

    public static function resolved(ProviderConnection $connection): self
    {
        return new self(
            resolved: true,
            connection: $connection,
            reasonCode: null,
            extensionReasonCode: null,
            message: null,
        );
    }

    public static function blocked(
        string $reasonCode,
        ?string $message = null,
        ?string $extensionReasonCode = null,
        ?ProviderConnection $connection = null,
    ): self {
        return new self(
            resolved: false,
            connection: $connection,
            reasonCode: ProviderReasonCodes::isKnown($reasonCode) ? $reasonCode : ProviderReasonCodes::UnknownError,
            extensionReasonCode: $extensionReasonCode,
            message: $message,
        );
    }

    public function effectiveReasonCode(): string
    {
        return $this->reasonCode ?? ProviderReasonCodes::UnknownError;
    }
}
