using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NT8.Core.Analytics;
using NT8.Core.Common.Models;
using NT8.Core.Intelligence;
using NT8.Core.Logging;

namespace NT8.Core.Tests.Analytics
{
    [TestClass]
    public class TradeRecorderTests
    {
        private TradeRecorder _target;

        [TestInitialize]
        public void TestInitialize()
        {
            _target = new TradeRecorder(new BasicLogger("TradeRecorderTests"));
        }

        [TestMethod] public void RecordEntry_StoresTrade() { _target.RecordEntry("T1", Intent(), Fill(1, 100), Score(), RiskMode.PCP); Assert.IsNotNull(_target.GetTrade("T1")); }
        [TestMethod] public void RecordExit_SetsExitFields() { _target.RecordEntry("T2", Intent(), Fill(1, 100), Score(), RiskMode.PCP); _target.RecordExit("T2", Fill(1, 104)); Assert.IsTrue(_target.GetTrade("T2").ExitPrice.HasValue); }
        [TestMethod] public void RecordPartialFill_DoesNotThrow() { _target.RecordPartialFill("T3", Fill(1, 100)); Assert.IsTrue(true); }
        [TestMethod] public void GetTradesByGrade_Filters() { _target.RecordEntry("T4", Intent(), Fill(1, 100), Score(TradeGrade.A), RiskMode.PCP); Assert.AreEqual(1, _target.GetTradesByGrade(TradeGrade.A).Count); }
        [TestMethod] public void GetTradesByStrategy_Filters() { var i = Intent(); i.Metadata.Add("strategy_name", "S1"); _target.RecordEntry("T5", i, Fill(1, 100), Score(), RiskMode.PCP); Assert.AreEqual(1, _target.GetTradesByStrategy("S1").Count); }
        [TestMethod] public void GetTrades_ByDateRange_Filters() { _target.RecordEntry("T6", Intent(), Fill(1, 100), Score(), RiskMode.PCP); var list = _target.GetTrades(DateTime.UtcNow.AddMinutes(-1), DateTime.UtcNow.AddMinutes(1)); Assert.IsTrue(list.Count >= 1); }
        [TestMethod] public void ExportToCsv_HasHeader() { _target.RecordEntry("T7", Intent(), Fill(1, 100), Score(), RiskMode.PCP); var csv = _target.ExportToCsv(); StringAssert.Contains(csv, "TradeId,Symbol"); }
        [TestMethod] public void ExportToJson_HasArray() { _target.RecordEntry("T8", Intent(), Fill(1, 100), Score(), RiskMode.PCP); var json = _target.ExportToJson(); StringAssert.StartsWith(json, "["); }
        [TestMethod] public void GetTrade_Unknown_ReturnsNull() { Assert.IsNull(_target.GetTrade("NONE")); }
        [TestMethod] public void RecordExit_Unknown_Throws() { Assert.ThrowsException<ArgumentException>(() => _target.RecordExit("X", Fill(1, 100))); }
        [TestMethod] public void RecordEntry_NullIntent_Throws() { Assert.ThrowsException<ArgumentNullException>(() => _target.RecordEntry("T9", null, Fill(1, 100), Score(), RiskMode.PCP)); }
        [TestMethod] public void RecordEntry_NullFill_Throws() { Assert.ThrowsException<ArgumentNullException>(() => _target.RecordEntry("T10", Intent(), null, Score(), RiskMode.PCP)); }
        [TestMethod] public void RecordEntry_NullScore_Throws() { Assert.ThrowsException<ArgumentNullException>(() => _target.RecordEntry("T11", Intent(), Fill(1, 100), null, RiskMode.PCP)); }
        [TestMethod] public void GetTradesByStrategy_Empty_Throws() { Assert.ThrowsException<ArgumentNullException>(() => _target.GetTradesByStrategy("")); }
        [TestMethod] public void RecordExit_ComputesPnL() { _target.RecordEntry("T12", Intent(), Fill(1, 100), Score(), RiskMode.PCP); _target.RecordExit("T12", Fill(1, 110)); Assert.IsTrue(_target.GetTrade("T12").RealizedPnL > 0); }

        private static StrategyIntent Intent()
        {
            return new StrategyIntent("ES", OrderSide.Buy, OrderType.Market, null, 8, 16, 0.8, "test", new Dictionary<string, object>());
        }

        private static ConfluenceScore Score(TradeGrade grade = TradeGrade.B)
        {
            return new ConfluenceScore(0.7, 0.7, grade, new List<ConfluenceFactor>(), DateTime.UtcNow, new Dictionary<string, object>());
        }

        private static OrderFill Fill(int qty, double price)
        {
            return new OrderFill("O1", "ES", qty, price, DateTime.UtcNow, 1.0, Guid.NewGuid().ToString());
        }
    }
}

