using PolyTrader.Modules.CopyTrading.Logic;
using Xunit;
using static PolyTrader.Modules.CopyTrading.Logic.TradeMath;

namespace PolyTrader.Tests
{
    /// <summary>
    /// Sicherheitsnetz für die geldkritische Trade-Mathematik (PnL, PnL-%, gewichteter Entry).
    /// </summary>
    public class TradeMathTests
    {
        [Fact]
        public void ExitValue_is_shares_times_price()
        {
            Assert.Equal(99m, ExitValue(100m, 0.99m));
        }

        [Fact]
        public void RealizedPnl_profit_and_loss()
        {
            Assert.Equal(49m, RealizedPnl(sizeShares: 100m, exitPrice: 0.99m, entryAmountUsd: 50m));
            Assert.Equal(-20m, RealizedPnl(sizeShares: 100m, exitPrice: 0.40m, entryAmountUsd: 60m));
        }

        [Fact]
        public void PnlPercent_relative_to_entry_capital()
        {
            Assert.Equal(98m, PnlPercent(realizedPnl: 49m, entryAmountUsd: 50m));
            Assert.Equal(-20m, PnlPercent(realizedPnl: -20m, entryAmountUsd: 100m));
        }

        [Fact]
        public void PnlPercent_zero_entry_returns_zero_not_divide_by_zero()
        {
            Assert.Equal(0m, PnlPercent(49m, 0m));
        }

        [Fact]
        public void WeightedAverageEntryPrice_is_amount_over_size()
        {
            Assert.Equal(0.5m, WeightedAverageEntryPrice(totalAmountUsd: 100m, totalSize: 200m));
            Assert.Equal(0.51m, WeightedAverageEntryPrice(51m, 100m));
        }

        [Fact]
        public void WeightedAverageEntryPrice_zero_size_returns_zero()
        {
            Assert.Equal(0m, WeightedAverageEntryPrice(100m, 0m));
        }
    }
}
