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

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

        [Fact]
        public void Insert_and_Find_returns_all()
        {
            var repo = NewRepo();
            repo.Insert(new ClosedTrade { TradeId = 1, AccountId = 1, TokenId = "a" });
            repo.Insert(new ClosedTrade { TradeId = 2, AccountId = 1, TokenId = "b" });

            Assert.Equal(2, repo.Find(_ => true).Count);
        }

        [Fact]
        public void Exists_is_true_for_matching_account_and_token()
        {
            var repo = NewRepo();
            repo.Insert(new ClosedTrade { TradeId = 1, AccountId = 5, TokenId = "tok" });

            Assert.True(repo.Exists(5, "tok"));
            Assert.False(repo.Exists(5, "other"));
            Assert.False(repo.Exists(9, "tok"));
        }

        [Fact]
        public void Find_applies_predicate()
        {
            var repo = NewRepo();
            repo.Insert(new ClosedTrade { TradeId = 1, AccountId = 1, RealizedPnl = 10m });
            repo.Insert(new ClosedTrade { TradeId = 2, AccountId = 1, RealizedPnl = -3m });

            var winners = repo.Find(t => t.RealizedPnl > 0);
            Assert.Single(winners);
            Assert.Equal(1, winners[0].TradeId);
        }
    }
}
