using System.Text.Json;

namespace MBN_STOCK_WEBVIEW.LegacyBridge.Tests;

public sealed class LegacyUc4Uc6CatalogBridgeTests
{
    [Theory]
    [InlineData("begin-uc4-theme-catalog", typeof(LegacyBeginUc4ThemeCatalogIntent))]
    [InlineData("edit-uc4-selected-theme", typeof(LegacyEditUc4SelectedThemeIntent))]
    [InlineData("delete-uc4-selected-theme", typeof(LegacyDeleteUc4SelectedThemeIntent))]
    [InlineData("begin-uc6-expert-catalog", typeof(LegacyBeginUc6ExpertCatalogIntent))]
    [InlineData("edit-uc6-selected-expert", typeof(LegacyEditUc6SelectedExpertIntent))]
    [InlineData("delete-uc6-selected-expert", typeof(LegacyDeleteUc6SelectedExpertIntent))]
    public void Original_Uc4_Uc6_entries_accept_only_an_empty_payload(
        string command,
        Type expectedType)
    {
        var valid = JsonSerializer.Serialize(new
        {
            type = command,
            payload = new { }
        });
        Assert.True(LegacyBridgeProtocol.TryParseIntent(
            valid,
            out var parsed,
            out _));
        Assert.IsType(expectedType, parsed);

        var invalid = JsonSerializer.Serialize(new
        {
            type = command,
            payload = new { themeCode = "00000001" }
        });
        Assert.False(LegacyBridgeProtocol.TryParseIntent(
            invalid,
            out _,
            out _));
    }

    [Fact]
    public void Recommendation_amount_edit_accepts_one_opaque_row_and_web_safe_integer()
    {
        var valid = JsonSerializer.Serialize(new
        {
            type = "set-operator-catalog-draft-buy-amount",
            payload = new { rowId = "catalog-draft-0123456789abcdef", buyAmount = 71_000 }
        });

        Assert.True(LegacyBridgeProtocol.TryParseIntent(valid, out var parsed, out _));
        var amount = Assert.IsType<LegacySetOperatorCatalogDraftBuyAmountIntent>(parsed);
        Assert.Equal(71_000, amount.BuyAmount);

        foreach (var invalid in new[]
                 {
                     "{\"type\":\"set-operator-catalog-draft-buy-amount\",\"payload\":{\"rowId\":\"../unsafe\",\"buyAmount\":71000}}",
                     "{\"type\":\"set-operator-catalog-draft-buy-amount\",\"payload\":{\"rowId\":\"catalog-draft-0123456789abcdef\",\"buyAmount\":0}}",
                     "{\"type\":\"set-operator-catalog-draft-buy-amount\",\"payload\":{\"rowId\":\"catalog-draft-0123456789abcdef\",\"buyAmount\":71000,\"expertCode\":\"0001\"}}"
                 })
        {
            Assert.False(LegacyBridgeProtocol.TryParseIntent(invalid, out _, out _));
        }
    }

    [Theory]
    [InlineData("null", null)]
    [InlineData("71000", 71_000)]
    public void Stock_double_click_accepts_only_opaque_result_and_optional_amount(
        string amountJson,
        int? expected)
    {
        var json = JsonSerializer.Serialize(new
        {
            type = "activate-operator-catalog-stock",
            payload = new
            {
                resultId = "catalog-stock-0123456789abcdef",
                buyAmount = amountJson == "null" ? (decimal?)null : decimal.Parse(amountJson)
            }
        });

        Assert.True(LegacyBridgeProtocol.TryParseIntent(json, out var parsed, out _));
        var activation = Assert.IsType<LegacyActivateOperatorCatalogStockIntent>(parsed);
        Assert.Equal(expected.HasValue ? Convert.ToDecimal(expected.Value) : null,
            activation.BuyAmount);
    }
}
