"""趋势计划结束:须写入 trade_records(三所统一)."""
from __future__ import annotations

import inspect
import sqlite3
import sys
import unittest
from pathlib import Path
from unittest.mock import MagicMock

ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))

from lib.strategy.strategy_trend_register import _call_insert_trade_record  # noqa: E402


class _GateBotLikeModule:
    """模拟 gate:曾有 trend_plan_id 但缺 entry_reason 参数."""

    @staticmethod
    def insert_trade_record(
        conn,
        symbol,
        monitor_type,
        direction,
        trigger_price,
        stop_loss,
        initial_stop_loss=None,
        take_profit=None,
        margin_capital=None,
        leverage=None,
        pnl_amount=0,
        hold_seconds=0,
        trade_style=None,
        risk_amount=None,
        planned_rr=None,
        actual_rr=None,
        result="",
        miss_reason=None,
        opened_at=None,
        opened_at_ms=None,
        closed_at=None,
        closed_at_ms=None,
        exchange_trade_id=None,
        trend_plan_id=None,
    ):
        conn.execute(
            "INSERT INTO trade_records (symbol, monitor_type, direction, result, trend_plan_id) "
            "VALUES (?,?,?,?,?)",
            (symbol, monitor_type, direction, result, trend_plan_id),
        )


class TestTrendFinalizeTradeRecord(unittest.TestCase):
    def test_call_insert_filters_unknown_entry_reason(self):
        conn = sqlite3.connect(":memory:")
        conn.execute(
            "CREATE TABLE trade_records (symbol TEXT, monitor_type TEXT, direction TEXT, "
            "result TEXT, trend_plan_id INTEGER)"
        )
        m = _GateBotLikeModule()
        _call_insert_trade_record(
            m,
            4,
            dict(
                conn=conn,
                symbol="ONDO/USDT",
                monitor_type="趋势回调",
                direction="long",
                trigger_price=0.35,
                stop_loss=0.329,
                result="止损",
                entry_reason="趋势回调",
            ),
        )
        row = conn.execute(
            "SELECT symbol, monitor_type, trend_plan_id FROM trade_records"
        ).fetchone()
        self.assertEqual(row[0], "ONDO/USDT")
        self.assertEqual(row[1], "趋势回调")
        self.assertEqual(row[2], 4)

    def test_gate_insert_accepts_entry_reason(self):
        from crypto_monitor_gate import app as gate_app  # noqa: E402

        sig = inspect.signature(gate_app.insert_trade_record)
        self.assertIn("entry_reason", sig.parameters)
        self.assertIn("trend_plan_id", sig.parameters)


if __name__ == "__main__":
    unittest.main()
