using Moq;
using QuantEngine.Application.Services;
using QuantEngine.Core.Interfaces;
using QuantEngine.Core.Models;

namespace QuantEngine.Core.Tests;

public class WorkspaceApprovalServiceTests
{
    [Fact]
    public async Task WorkspaceService_RejectsBlankHistoryDomain()
    {
        var service = new WorkspaceService(new Mock<IWorkspaceRepository>().Object, new Mock<IPostgresqlHistoryStore>().Object);

        await Assert.ThrowsAsync<ArgumentException>(() =>
            service.AppendHistoryAsync("  ", new Dictionary<string, object?>()));
    }

    [Fact]
    public async Task ApprovalService_NormalizesLookupKeys()
    {
        var repo = new Mock<IWorkspaceRepository>(MockBehavior.Strict);
        repo.Setup(r => r.GetApprovalAsync("workflow", "target-1")).ReturnsAsync(new WorkspaceApproval());

        var service = new ApprovalService(repo.Object);

        var approval = await service.GetApprovalAsync(" workflow ", " target-1 ");

        Assert.NotNull(approval);
        repo.VerifyAll();
    }

    [Fact]
    public async Task ApprovalService_RejectsBlankLockTarget()
    {
        var service = new ApprovalService(new Mock<IWorkspaceRepository>().Object);

        await Assert.ThrowsAsync<ArgumentException>(() =>
            service.ReleaseLockAsync("workflow", " "));
    }
}
