"""Tests for jason_utils.performance module."""

import os
import tempfile

import pandas as pd
import pytest

from jason_utils.performance import generate_performance_html


@pytest.fixture
def sample_trades():
    """建立測試用交易資料。"""
    return pd.DataFrame({
        "entry_date": [
            "2024-01-05", "2024-01-10", "2024-01-15",
            "2024-02-03", "2024-02-10", "2024-03-01",
        ],
        "stock_code": ["2330", "2317", "2454", "2330", "2317", "2454"],
        "profit_pct": [0.02, -0.01, 0.03, 0.015, -0.005, 0.025],
    })


@pytest.fixture
def sample_summary():
    """建立測試用績效摘要。"""
    return {
        "total_trades": 6,
        "win_rate_pct": 66.67,
        "total_return_pct": 7.5,
        "annual_return_pct": 45.0,
        "max_drawdown_pct": -1.0,
    }


class TestGeneratePerformanceHtml:
    def test_generates_html_file(self, sample_trades, sample_summary):
        with tempfile.TemporaryDirectory() as tmpdir:
            output = os.path.join(tmpdir, "report.html")
            result = generate_performance_html(
                sample_trades, sample_summary,
                output_path=output, open_browser=False,
            )
            assert result == output
            assert os.path.exists(output)
            content = open(output).read()
            assert "plotly" in content.lower()

    def test_with_params(self, sample_trades, sample_summary):
        with tempfile.TemporaryDirectory() as tmpdir:
            output = os.path.join(tmpdir, "report.html")
            params = {"策略": "新高突破", "持有天數": 3}
            result = generate_performance_html(
                sample_trades, sample_summary,
                params=params, output_path=output, open_browser=False,
            )
            assert os.path.exists(output)
            content = open(output).read()
            assert "新高突破" in content

    def test_empty_trades(self, sample_summary):
        empty = pd.DataFrame(columns=["entry_date", "profit_pct"])
        with tempfile.TemporaryDirectory() as tmpdir:
            output = os.path.join(tmpdir, "report.html")
            result = generate_performance_html(
                empty, sample_summary,
                output_path=output, open_browser=False,
            )
            assert result == output
            assert not os.path.exists(output)

    def test_without_params(self, sample_trades, sample_summary):
        with tempfile.TemporaryDirectory() as tmpdir:
            output = os.path.join(tmpdir, "report.html")
            generate_performance_html(
                sample_trades, sample_summary,
                params=None, output_path=output, open_browser=False,
            )
            assert os.path.exists(output)

    def test_summary_values_in_html(self, sample_trades, sample_summary):
        with tempfile.TemporaryDirectory() as tmpdir:
            output = os.path.join(tmpdir, "report.html")
            generate_performance_html(
                sample_trades, sample_summary,
                output_path=output, open_browser=False,
            )
            content = open(output).read()
            assert "66.67" in content
            assert "45.0" in content
