using System.Text.Json.Nodes;
using Eis.Infrastructure.Administration;

namespace Eis.Infrastructure.Tests.Administration;

public sealed class AdminNoticeServiceTests
{
    [Theory]
    [InlineData("publicVisible")]
    [InlineData("visible")]
    public void TryReadPublicVisibility_AcceptsCurrentAndLegacyKeys(string key)
    {
        var body = new JsonObject { [key] = false };

        var parsed = AdminNoticeService.TryReadPublicVisibility(body, out var visible);

        Assert.True(parsed);
        Assert.False(visible);
    }

    [Fact]
    public void TryReadPublicVisibility_RejectsMissingOrNonBooleanValue()
    {
        Assert.False(AdminNoticeService.TryReadPublicVisibility(new JsonObject(), out _));
        Assert.False(AdminNoticeService.TryReadPublicVisibility(
            new JsonObject { ["publicVisible"] = "false" },
            out _));
    }

    [Fact]
    public void PublicationProjector_ExposesConsistentVisibilityFields()
    {
        var payload = new JsonObject
        {
            ["publicVisible"] = false,
            ["reviewedAt"] = "2026-07-23T00:00:00.000Z"
        };
        var snapshot = new AdminNoticeManagementSnapshot(
            [],
            [new AdminPublicationRecord(
                "plan_1", "plan", "exam_1", null, "school_1", "approved", payload,
                "2026-07-23T00:00:00.000Z", "2026-07-23T00:00:00.000Z")],
            new Dictionary<string, string> { ["exam_1"] = "统一考试" },
            new Dictionary<string, string> { ["school_1"] = "第一中学" });

        var publication = Assert.Single(AdminPublicationProjector.Build(snapshot));

        Assert.False(publication["publicVisible"]!.GetValue<bool>());
        Assert.False(publication["visible"]!.GetValue<bool>());
        Assert.Equal("hidden", publication["status"]!.GetValue<string>());
    }
}
