using CashLineERP.Application.Workflow;
using CashLineERP.Infrastructure.Persistence.Repositories;

namespace CashLineERP.Api.Tests;

public sealed class WorkflowAdministrationServiceQaTests
{
    [Fact]
    public void Workflow_Administration_Reads_Rules_And_Instance_Detail()
    {
        using var dbContext = QaDbContextFactory.CreateContext();

        var service = new WorkflowService(new WorkflowReadRepository(dbContext));

        var activeRules = service.GetWorkflowRules(activeOnly: true);
        Assert.Contains(activeRules, rule =>
            rule.ModuleCode == "PROCUREMENT" &&
            rule.DocumentType == "PURCHASE_REQUISITION" &&
            rule.IsActive);

        var instances = service.GetWorkflowInstances(
            moduleCode: "PROCUREMENT",
            documentType: "PURCHASE_REQUISITION",
            statusCode: "SUBMITTED");

        var instance = Assert.Single(instances);
        Assert.Equal("PR-000001", instance.DocumentNumber);
        Assert.Equal("OPS_MKR01", instance.MakerLoginName);

        var detail = service.GetWorkflowInstance(instance.WorkflowInstanceId);
        Assert.NotNull(detail);
        Assert.Equal(instance.WorkflowInstanceId, detail!.WorkflowInstanceId);
        Assert.Equal("SUBMITTED", detail.CurrentStatusCode);
        Assert.NotEmpty(detail.Actions);
    }
}
