using System.Text.Json;
using Google.Protobuf;
using ZB.MOM.WW.MxGateway.Client;
using ZB.MOM.WW.MxGateway.Contracts.Proto;

namespace ZB.MOM.WW.MxGateway.Client.Tests;

public sealed class MxStatusProxyExtensionsTests
{
    /// <summary>Verifies that fixture statuses correctly project success and preserve raw integer fields.</summary>
    [Fact]
    public void FixtureStatuses_ProjectSuccessAndPreserveRawFields()
    {
        using JsonDocument document = JsonDocument.Parse(ReadFixture(
            "statuses",
            "status-conversion-cases.json"));

        foreach (JsonElement testCase in document.RootElement.GetProperty("cases").EnumerateArray())
        {
            MxStatusProxy status = JsonParser.Default.Parse<MxStatusProxy>(
                testCase.GetProperty("status").GetRawText());
            int success = testCase.GetProperty("status").GetProperty("success").GetInt32();

            Assert.Equal(success != 0 && status.Category is MxStatusCategory.Ok, status.IsSuccess());
            Assert.Equal(
                testCase.GetProperty("status").GetProperty("rawCategory").GetInt32(),
                status.RawCategory);
            Assert.Equal(
                testCase.GetProperty("status").GetProperty("rawDetectedBy").GetInt32(),
                status.RawDetectedBy);
        }
    }

    private static string ReadFixture(string category, string fileName)
    {
        DirectoryInfo directory = new(AppContext.BaseDirectory);
        while (directory is not null)
        {
            string path = Path.Combine(
                directory.FullName,
                "clients",
                "proto",
                "fixtures",
                "behavior",
                category,
                fileName);

            if (File.Exists(path))
            {
                return File.ReadAllText(path);
            }

            directory = directory.Parent!;
        }

        throw new FileNotFoundException(fileName);
    }
}
