using System;
using System.IO;
using StellaOps.Concelier.WebService.Options;
using Xunit;

namespace StellaOps.Concelier.WebService.Tests;

public sealed class ConcelierOptionsPostConfigureTests
{
    [Fact]
    public void Apply_LoadsClientSecretFromRelativeFile()
    {
        var tempDirectory = Directory.CreateTempSubdirectory();
        try
        {
            var secretPath = Path.Combine(tempDirectory.FullName, "authority.secret");
            File.WriteAllText(secretPath, " concelier-secret ");

            var options = new ConcelierOptions
            {
                Authority = new ConcelierOptions.AuthorityOptions
                {
                    ClientSecretFile = "authority.secret"
                }
            };

            ConcelierOptionsPostConfigure.Apply(options, tempDirectory.FullName);

            Assert.Equal("concelier-secret", options.Authority.ClientSecret);
        }
        finally
        {
            if (Directory.Exists(tempDirectory.FullName))
            {
                Directory.Delete(tempDirectory.FullName, recursive: true);
            }
        }
    }

    [Fact]
    public void Apply_ThrowsWhenSecretFileMissing()
    {
        var options = new ConcelierOptions
        {
            Authority = new ConcelierOptions.AuthorityOptions
            {
                ClientSecretFile = "missing.secret"
            }
        };

        var exception = Assert.Throws<InvalidOperationException>(() =>
            ConcelierOptionsPostConfigure.Apply(options, AppContext.BaseDirectory));

        Assert.Contains("Authority client secret file", exception.Message);
    }
}
