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

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

        [Fact]
        public void GetRecent_orders_by_closed_at_descending_and_limits()
        {
            var repo = NewRepo();
            var t = new DateTime(2026, 7, 1, 0, 0, 0, DateTimeKind.Utc);
            repo.Insert(new TradeRecord { Id = "a", ClosedAt = t.AddHours(1) });
            repo.Insert(new TradeRecord { Id = "b", ClosedAt = t.AddHours(3) });
            repo.Insert(new TradeRecord { Id = "c", ClosedAt = t.AddHours(2) });

            var recent = repo.GetRecent(2);

            Assert.Equal(2, recent.Count);
            Assert.Equal("b", recent[0].Id);
            Assert.Equal("c", recent[1].Id);
        }

        [Fact]
        public void Find_filters_by_module_name()
        {
            var repo = NewRepo();
            repo.Insert(new TradeRecord { Id = "a", ModuleName = "CopyTrading" });
            repo.Insert(new TradeRecord { Id = "b", ModuleName = "Other" });

            var ct = repo.Find(x => x.ModuleName == "CopyTrading");
            Assert.Single(ct);
            Assert.Equal("a", ct[0].Id);
        }

        [Fact]
        public void Generated_id_is_unique_per_record()
        {
            var a = new TradeRecord();
            var b = new TradeRecord();
            Assert.False(string.IsNullOrEmpty(a.Id));
            Assert.NotEqual(a.Id, b.Id);
        }
    }
}
