using Baya.Domain.Entities.SupportAlerts;
using Baya.Infrastructure.Persistence.Services.SupportAlerts;

namespace Baya.Test.Foundation.Marketplace;

public sealed class SupportAlertServiceTests
{
    [Fact]
    public async Task Raise_List_Assign_Resolve_Lifecycle()
    {
        using var host = new OpsTestHost();
        var admin = await host.AddUserAsync("admin");
        var service = new SupportAlertService(host.Db, host.Clock);

        var alertId = await service.RaiseAsync(
            SupportAlertType.LowRating, "review", "42", SupportAlertSeverity.High, reviewId: 42);

        var open = await service.ListAsync(null, SupportAlertStatus.Open, null, 1, 20);
        var raised = Assert.Single(open.Items);
        Assert.Equal(alertId, raised.Id);
        Assert.Equal(42, raised.ReviewId);
        Assert.Equal(SupportAlertStatus.Open, raised.Status);

        Assert.True(await service.AssignAsync(alertId, admin));
        var assigned = (await service.ListAsync(null, SupportAlertStatus.Assigned, null, 1, 20)).Items.Single();
        Assert.Equal(admin, assigned.OwnerUserId);

        Assert.True(await service.ResolveAsync(alertId, "Handled — nurse contacted."));
        var resolved = (await service.ListAsync(null, SupportAlertStatus.Resolved, null, 1, 20)).Items.Single();
        Assert.Equal("Handled — nurse contacted.", resolved.ResolutionNote);
        Assert.NotNull(resolved.ResolvedAt);

        // Forward-only: a resolved alert cannot be resolved again.
        Assert.False(await service.ResolveAsync(alertId, "again"));
    }
}
