using System.Text.Json;
using Elfisa.Core;
using Xunit;

namespace Elfisa.Tests;

public class DtoTests
{
    [Fact]
    public void PriceItem_deserializes_snake_case()
    {
        var json = "{\"supplier_price_id\":\"sp1\",\"drug_name\":\"Аспирин\",\"barcode\":\"123\"," +
                   "\"price\":12.30,\"quantity\":5,\"es_code\":777,\"trade_name\":\"Асп\"}";
        var it = JsonSerializer.Deserialize<PriceItem>(json)!;
        Assert.Equal("sp1", it.SupplierPriceId);
        Assert.Equal("Аспирин", it.DrugName);
        Assert.Equal("123", it.Barcode);
        Assert.Equal(12.30m, it.Price);
        Assert.Equal(5m, it.Quantity);
        Assert.Equal(777, it.EsCode);
        Assert.Equal("Асп", it.TradeName);
    }

    [Fact]
    public void BuyerOrderCreateRequest_serializes_snake_case()
    {
        var req = new BuyerOrderCreateRequest
        {
            LocationId = "loc1",
            GlobalSign = "EX-1",
            Items = { new BuyerOrderItemReq { SupplierPriceId = "sp1", Qty = 2, ItemName = "Товар", Barcode = "111" } }
        };
        var json = JsonSerializer.Serialize(req);
        Assert.Contains("\"location_id\":\"loc1\"", json);
        Assert.Contains("\"supplier_price_id\":\"sp1\"", json);
        Assert.Contains("\"qty\":2", json);
    }

    [Fact]
    public void LoginResponse_round_trip()
    {
        var r = JsonSerializer.Deserialize<LoginResponse>(
            "{\"token\":\"t\",\"role\":\"manager\",\"redirect_url\":\"/x\"}")!;
        Assert.Equal("t", r.Token);
        Assert.Equal("manager", r.Role);
        Assert.Equal("/x", r.RedirectUrl);
    }

    [Fact]
    public void AppSettings_round_trip_preserves_fields()
    {
        var s = new AppSettings { ApiBaseUrl = "https://x", Theme = "Dark", FontFamily = "Arial", UiScale = 1.3, Username = "u" };
        var back = JsonSerializer.Deserialize<AppSettings>(JsonSerializer.Serialize(s))!;
        Assert.Equal("https://x", back.ApiBaseUrl);
        Assert.Equal("Dark", back.Theme);
        Assert.Equal("Arial", back.FontFamily);
        Assert.Equal(1.3, back.UiScale);
        Assert.Equal("u", back.Username);
    }
}
