from __future__ import annotations

import json
from pathlib import Path

from src.tools.tradingview_community_ideas import (
    attach_price_snapshot,
    classify_idea_bias,
    parse_ideas_html,
    summarize_community_ideas,
    write_ideas_snapshot,
)


HTML = """
<html><body>
<a href="https://www.tradingview.com/chart/BTCUSDT/abc-BTC-breakout-next/">BTC breakout toward 70000 next?</a>
<a href="https://www.tradingview.com/chart/BTCUSDT/abc-BTC-breakout-next/">Bitcoin holds demand zone and could rally after breakout confirmation.</a>
<a href="https://www.tradingview.com/chart/BTCUSDT/abc-BTC-breakout-next/#chart-view-comment-form">comments</a>
<a href="https://www.tradingview.com/chart/BTCUSDT/def-BTC-dump-risk/">BTC downside risk below support</a>
<a href="https://www.tradingview.com/chart/BTCUSDT/def-BTC-dump-risk/">Breakdown can trigger bearish continuation and short setup.</a>
<a href="https://www.tradingview.com/symbols/BTCUSDT/">BTCUSDT</a>
</body></html>
"""


def test_parse_ideas_html_dedupes_chart_links_and_keeps_research_only() -> None:
    ideas = parse_ideas_html(HTML, coin="BTC", source_url="https://example.test", limit=10)

    assert len(ideas) == 2
    assert ideas[0]["coin"] == "BTC"
    assert ideas[0]["author"] == "unknown"
    assert ideas[0]["research_only"] is True
    assert ideas[0]["live_order_allowed"] is False
    assert ideas[0]["mainnet_signed_action"] is False
    assert ideas[0]["title"] == "BTC breakout toward 70000 next?"
    assert "demand zone" in ideas[0]["excerpt"]
    assert ideas[0]["bias"] == "bullish"
    assert ideas[1]["bias"] == "bearish"


def test_classify_idea_bias_handles_mixed_and_neutral() -> None:
    assert classify_idea_bias("strong rally breakout long") == "bullish"
    assert classify_idea_bias("bearish breakdown short downside") == "bearish"
    assert classify_idea_bias("support and resistance range watch") == "neutral"
    assert classify_idea_bias("rally but downside risk") == "mixed"


def test_attach_price_snapshot_uses_hyperliquid_coin_mids() -> None:
    ideas = parse_ideas_html(HTML, coin="BTC", source_url="https://example.test", limit=1)
    enriched = attach_price_snapshot(ideas, {"BTC": "60000.5"})

    assert enriched[0]["price_at_capture_usd"] == "60000.5"
    assert enriched[0]["price_source"] == "hyperliquid_all_mids"
    assert enriched[0]["live_order_allowed"] is False


def test_write_and_summarize_community_ideas(tmp_path: Path) -> None:
    ideas = parse_ideas_html(HTML, coin="BTC", source_url="https://example.test", limit=10)
    path = write_ideas_snapshot(ideas, runtime_dir=tmp_path)

    assert path == tmp_path / "research" / "tradingview_community_ideas.jsonl"
    rows = [json.loads(line) for line in path.read_text().splitlines()]
    assert len(rows) == 2
    assert all(row["research_only"] is True for row in rows)

    summary = summarize_community_ideas(tmp_path)
    assert summary["total"] == 2
    assert summary["by_coin"] == {"BTC": 2}
    assert summary["by_bias"] == {"bullish": 1, "bearish": 1}
    assert summary["live_order_allowed_count"] == 0
    assert summary["recommendation"] == "research_only_never_trade_directly"
