using MBN_STOCK_WEBVIEW.Core.Playout.Scenes;
using MBN_STOCK_WEBVIEW.LegacyApplication;

namespace MBN_STOCK_WEBVIEW.LegacyApplication.Tests;

public sealed class LegacyStockCutCatalogTests
{
    [Fact]
    public void Rows_MatchRuntimeAssetPhysicalLayout()
    {
        Assert.Equal(33, LegacyStockCutCatalog.Rows.Count);
        Assert.Equal(31, LegacyStockCutCatalog.Rows.Count(row => !row.IsSeparator));
        Assert.Equal([3, 8], LegacyStockCutCatalog.Rows
            .Where(row => row.IsSeparator)
            .Select(row => row.PhysicalIndex));
        Assert.Equal("1열판기본_시간외단일가", LegacyStockCutCatalog.Rows[2].RawLabel);
        Assert.Equal(31, LegacyStockCutCatalog.Rows[^1].DisplayOrdinal);
        Assert.Equal(
            "45DFB1804828F0E4A45F73D681AF0709CE5662872FAA49CFC9F7A0B940409E20",
            LegacyStockCutCatalog.RuntimeAssetSha256);
    }

    [Fact]
    public void EveryActiveCut_ResolvesToRegisteredAlias()
    {
        foreach (var row in LegacyStockCutCatalog.Rows.Where(row => !row.IsSeparator))
        {
            var alias = LegacyStockCutCatalog.ResolveCutAlias(row.RawLabel, isNxt: false);
            Assert.NotEmpty(LegacySceneCatalog.FindByCutAlias(alias));
        }
    }

    [Theory]
    [InlineData("1열판기본_현재가", "N5001")]
    public void NxtCurrent_UsesN5001(string label, string expected)
    {
        Assert.Equal(expected, LegacyStockCutCatalog.ResolveCutAlias(label, isNxt: true));
    }

    [Fact]
    public void PlaylistRow_PreservesLegacySevenFieldsAndDefersAliasResolution()
    {
        var stock = new LegacyStockIdentity(
            LegacyDomesticStockMarket.Kospi,
            "삼성전자",
            "005930");
        var row = LegacyStockCutCatalog.CreatePlaylistRow(
            "row-1",
            stock.DisplayName,
            stock,
            LegacyStockCutCatalog.Rows[1]);

        Assert.True(row.IsEnabled);
        Assert.Equal("코스피", row.MarketText);
        Assert.Equal("삼성전자", row.StockName);
        Assert.Equal("1열판기본", row.GraphicType);
        Assert.Equal("현재가", row.Subtype);
        Assert.Equal("1/1", row.PageText);
        Assert.Equal("005930", row.StockCode);

        var playout = row.ToPlayoutEntry();
        Assert.Equal("5001", playout.CutCode);
        Assert.Equal("005930", playout.Selection?.DataCode);
    }

    [Theory]
    [InlineData(5, "액면가", S5011DetailBranch.FaceValue)]
    [InlineData(6, "PBR", S5011DetailBranch.Valuation)]
    [InlineData(7, "거래량", S5011DetailBranch.Volume)]
    public void S5011_rows_preserve_visible_subtype_and_use_canonical_runtime_branch(
        int physicalIndex,
        string visibleSubtype,
        S5011DetailBranch runtimeBranch)
    {
        var stock = new LegacyStockIdentity(
            LegacyDomesticStockMarket.Kospi,
            "삼성전자",
            "005930");

        var row = LegacyStockCutCatalog.CreatePlaylistRow(
            "row-1",
            stock.DisplayName,
            stock,
            LegacyStockCutCatalog.Rows[physicalIndex]);

        Assert.Equal(visibleSubtype, row.Subtype);
        Assert.Equal(runtimeBranch.ToString(), row.PlayoutSelection?.Subtype);
        Assert.Equal("5011", row.ToPlayoutEntry().CutCode);
        Assert.Equal(runtimeBranch.ToString(), row.ToPlayoutEntry().Selection?.Subtype);
    }
}
