using System;
using StellaOps.Excititor.Connectors.OCI.OpenVEX.Attest.Configuration;

namespace StellaOps.Excititor.Connectors.OCI.OpenVEX.Attest.Authentication;

public enum OciRegistryAuthMode
{
    Anonymous = 0,
    Basic = 1,
    IdentityToken = 2,
    RefreshToken = 3,
}

public sealed record OciRegistryAuthorization(
    string? RegistryAuthority,
    OciRegistryAuthMode Mode,
    string? Username,
    string? Password,
    string? IdentityToken,
    string? RefreshToken,
    bool AllowAnonymousFallback)
{
    public static OciRegistryAuthorization Create(OciRegistryAuthenticationOptions options)
    {
        ArgumentNullException.ThrowIfNull(options);

        var mode = OciRegistryAuthMode.Anonymous;
        string? username = null;
        string? password = null;
        string? identityToken = null;
        string? refreshToken = null;

        if (!string.IsNullOrWhiteSpace(options.IdentityToken))
        {
            mode = OciRegistryAuthMode.IdentityToken;
            identityToken = options.IdentityToken;
        }
        else if (!string.IsNullOrWhiteSpace(options.RefreshToken))
        {
            mode = OciRegistryAuthMode.RefreshToken;
            refreshToken = options.RefreshToken;
        }
        else if (!string.IsNullOrWhiteSpace(options.Username))
        {
            mode = OciRegistryAuthMode.Basic;
            username = options.Username;
            password = options.Password;
        }

        return new OciRegistryAuthorization(
            RegistryAuthority: options.RegistryAuthority,
            Mode: mode,
            Username: username,
            Password: password,
            IdentityToken: identityToken,
            RefreshToken: refreshToken,
            AllowAnonymousFallback: options.AllowAnonymousFallback);
    }
}
