using ZB.MOM.WW.GalaxyRepository;

namespace ZB.MOM.WW.GalaxyRepository.Tests;

/// <summary>
///     Pure mapper tests for <see cref="GalaxyRepository.MapAlarmRow"/>. These assert the
///     FullTagReference / SourceObjectReference derivation produced by
///     <c>AlarmAttributesSql</c> without touching a database: the SQL projects
///     <c>tag_name</c> as the source object and <c>tag_name + '.' + attribute_name</c> as
///     the full reference, exactly as <c>AttributesSql</c> does.
/// </summary>
public sealed class GalaxyAlarmAttributeMappingTests
{
    /// <summary>Verifies the mapper copies all projected columns onto the row.</summary>
    [Fact]
    public void MapAlarmRow_CopiesProjectedColumns()
    {
        GalaxyAlarmAttributeRow row = GalaxyRepository.MapAlarmRow(
            fullTagReference: "Tank01.Level.HiHi",
            sourceObjectReference: "Tank01",
            area: "TestArea");

        Assert.Equal("Tank01.Level.HiHi", row.FullTagReference);
        Assert.Equal("Tank01", row.SourceObjectReference);
        Assert.Equal("TestArea", row.Area);
    }

    /// <summary>
    ///     Verifies <see cref="GalaxyAlarmAttributeRow.AckCommentSubtag"/> is always empty:
    ///     the schema does not expose an ack-comment address, so the watch-list resolver
    ///     composes it later from configuration.
    /// </summary>
    [Fact]
    public void MapAlarmRow_LeavesAckCommentSubtagEmpty()
    {
        GalaxyAlarmAttributeRow row = GalaxyRepository.MapAlarmRow(
            fullTagReference: "Tank01.Level.HiHi",
            sourceObjectReference: "Tank01",
            area: "TestArea");

        Assert.Equal(string.Empty, row.AckCommentSubtag);
    }

    /// <summary>
    ///     Verifies the SourceObjectReference is the owning object (the SQL <c>tag_name</c>),
    ///     i.e. the segment that precedes the first attribute dot in the full reference, even
    ///     when the attribute itself is a multi-segment extension path.
    /// </summary>
    [Theory]
    [InlineData("Tank01", "Level.HiHi", "Tank01.Level.HiHi")]
    [InlineData("Pump_001", "Speed", "Pump_001.Speed")]
    [InlineData("TestAlarm001", "Alarm001", "TestAlarm001.Alarm001")]
    public void MapAlarmRow_SourceObjectIsSegmentBeforeFirstAttributeDot(
        string tagName,
        string attributeName,
        string expectedFullReference)
    {
        // Mirror the AlarmAttributesSql projection: full_tag_reference = tag_name + '.' + attribute_name.
        string fullTagReference = tagName + "." + attributeName;

        GalaxyAlarmAttributeRow row = GalaxyRepository.MapAlarmRow(fullTagReference, tagName, area: "TestArea");

        Assert.Equal(expectedFullReference, row.FullTagReference);
        Assert.Equal(tagName, row.SourceObjectReference);
        Assert.Equal("TestArea", row.Area);
        Assert.Equal(row.FullTagReference, row.SourceObjectReference + "." + attributeName);
    }
}
