using System.Linq;
using PolyTrader.Core.Persistence.Ef;
using PolyTrader.Tests.TestSupport;
using PolyTraderSharp.Models;
using Xunit;

namespace PolyTrader.Tests
{
    public class AccountRepositoryTests
    {
        private static EfAccountRepository NewRepo() =>
            new(new InMemoryContextFactory<CoreDbContext>(o => new CoreDbContext(o)));

        [Fact]
        public void Upsert_inserts_and_reads_back_account()
        {
            var repo = NewRepo();
            repo.Upsert(new AccountState
            {
                AccountId = 1,
                Name = "Richard Test",
                WalletAddress = "0xabc",
                IsDemo = false,
                TotalBalance = 64.763626m
            });

            var acc = repo.GetAll().Single();
            Assert.Equal("Richard Test", acc.Name);
            Assert.Equal(64.763626m, acc.TotalBalance);
        }

        [Fact]
        public void Upsert_updates_existing_without_duplicating()
        {
            var repo = NewRepo();
            repo.Upsert(new AccountState { AccountId = 1, Name = "A" });
            repo.Upsert(new AccountState { AccountId = 1, Name = "B" });

            var all = repo.GetAll();
            Assert.Single(all);
            Assert.Equal("B", all[0].Name);
        }

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