using PolyTrader.Modules.CopyTrading.Persistence.Ef;
using PolyTrader.Tests.TestSupport;
using PolyTraderSharp.Models;
using Xunit;

namespace PolyTrader.Tests
{
    public class AccountSettingsRepositoryTests
    {
        private static EfCopyTradingAccountSettingsRepository NewRepo() =>
            new(new InMemoryContextFactory<CopyTradingDbContext>(o => new CopyTradingDbContext(o)));

        [Fact]
        public void Upsert_and_Get_persists_decimal_values()
        {
            var repo = NewRepo();
            repo.Upsert(new CopyTradingAccountSettings
            {
                AccountId = 1,
                PerMarketLimit = 0.5m,
                PerMasterLimit = 15m,
                MaxBuyPrice = 98m,
                PreRedeemLimit = 99.5m
            });

            var s = repo.Get(1);
            Assert.NotNull(s);
            Assert.Equal(0.5m, s!.PerMarketLimit);
            Assert.Equal(15m, s.PerMasterLimit);
            Assert.Equal(98m, s.MaxBuyPrice);
            Assert.Equal(99.5m, s.PreRedeemLimit);
        }

        [Fact]
        public void Get_returns_null_when_missing()
        {
            var repo = NewRepo();
            Assert.Null(repo.Get(42));
        }

        [Fact]
        public void Upsert_updates_existing_without_duplicating()
        {
            var repo = NewRepo();
            repo.Upsert(new CopyTradingAccountSettings { AccountId = 1, PerMarketLimit = 1m });
            repo.Upsert(new CopyTradingAccountSettings { AccountId = 1, PerMarketLimit = 2m });

            Assert.Single(repo.GetAll());
            Assert.Equal(2m, repo.Get(1)!.PerMarketLimit);
        }

        [Fact]
        public void Delete_removes_settings()
        {
            var repo = NewRepo();
            repo.Upsert(new CopyTradingAccountSettings { AccountId = 1 });
            repo.Delete(1);
            Assert.Empty(repo.GetAll());
        }
    }
}
