using System;
using System.Collections.Generic;
using ZB.MOM.WW.MxGateway.Contracts.Proto;
using ZB.MOM.WW.MxGateway.Worker.Conversion;

namespace ZB.MOM.WW.MxGateway.Worker.Tests.Conversion;

public sealed class MxStatusProxyConverterTests
{
    private readonly MxStatusProxyConverter _converter = new();

    /// <summary>Verifies that status struct fields are preserved during conversion.</summary>
    [Fact]
    public void Convert_WithStatusStruct_PreservesStatusFields()
    {
        FakeMxStatusProxy status = new()
        {
            success = 1,
            category = 5,
            detectedBy = 3,
            detail = 21,
        };

        MxStatusProxy converted = _converter.Convert(status);

        Assert.Equal(1, converted.Success);
        Assert.Equal(MxStatusCategory.OperationalError, converted.Category);
        Assert.Equal(MxStatusSource.RespondingNmx, converted.DetectedBy);
        Assert.Equal(21, converted.Detail);
        Assert.Equal(5, converted.RawCategory);
        Assert.Equal(3, converted.RawDetectedBy);
        Assert.Equal("Invalid reference", converted.DiagnosticText);
    }

    /// <summary>Verifies that status arrays are converted without collapsing duplicate entries.</summary>
    [Fact]
    public void ConvertMany_WithStatusArray_DoesNotCollapseEntries()
    {
        FakeMxStatusProxy[] statuses =
        [
            new()
            {
                success = 1,
                category = 0,
                detectedBy = 0,
                detail = 0,
            },
            new()
            {
                success = 0,
                category = 6,
                detectedBy = 5,
                detail = 33,
            },
        ];

        IReadOnlyList<MxStatusProxy> converted = _converter.ConvertMany(statuses);

        Assert.Equal(2, converted.Count);
        Assert.Equal(MxStatusCategory.Ok, converted[0].Category);
        Assert.Equal(MxStatusCategory.SecurityError, converted[1].Category);
        Assert.Equal(MxStatusSource.RespondingAutomationObject, converted[1].DetectedBy);
        Assert.Equal("Write access denied", converted[1].DiagnosticText);
    }

    /// <summary>Verifies that unknown category and source values preserve raw field values.</summary>
    [Fact]
    public void Convert_WithUnknownCategoryAndSource_PreservesRawFields()
    {
        FakeMxStatusProxy status = new()
        {
            success = -1,
            category = 99,
            detectedBy = 42,
            detail = 1234,
        };

        MxStatusProxy converted = _converter.Convert(status);

        Assert.Equal(-1, converted.Success);
        Assert.Equal(MxStatusCategory.Unknown, converted.Category);
        Assert.Equal(MxStatusSource.Unknown, converted.DetectedBy);
        Assert.Equal(99, converted.RawCategory);
        Assert.Equal(42, converted.RawDetectedBy);
        Assert.Equal(1234, converted.Detail);
        Assert.Equal(string.Empty, converted.DiagnosticText);
    }

    /// <summary>Verifies that completion-only status bytes are preserved as hex metadata.</summary>
    [Fact]
    public void PreserveCompletionOnlyStatusBytes_ReturnsRawHexMetadata()
    {
        string rawStatus = _converter.PreserveCompletionOnlyStatusBytes(
            [0x00, 0x00, 0x50, 0x80, 0x00]);

        Assert.Equal("completion_only_status_hex=0000508000", rawStatus);
    }

    /// <summary>Verifies that missing status fields throw a conversion exception.</summary>
    [Fact]
    public void Convert_WithMissingStatusField_ThrowsConversionException()
    {
        MxStatusConversionException exception =
            Assert.Throws<MxStatusConversionException>(() => _converter.Convert(new MissingFields()));

        Assert.Contains("success", exception.Message);
    }

    public struct FakeMxStatusProxy
    {
        public short success;

        public int category;

        public int detectedBy;

        public short detail;
    }

    private sealed class MissingFields
    {
    }
}
