import json
from datetime import datetime, timezone
from pathlib import Path


def test_append_journal_event_writes_jsonl_with_required_fields(tmp_path):
    from journal import append_journal_event, read_journal_events

    journal = tmp_path / "trade_journal.jsonl"
    event = append_journal_event(
        journal,
        event_type="entry",
        coin="BTC",
        side="long",
        price=100.0,
        size=0.5,
        dry_run=True,
        reason="flash_crash",
        ts=datetime(2026, 5, 28, 10, 0, tzinfo=timezone.utc),
    )

    assert event["event_type"] == "entry"
    assert event["coin"] == "BTC"
    assert event["dry_run"] is True
    assert event["realized_pnl_usd"] == 0.0
    events = read_journal_events(journal)
    assert events == [event]


def test_daily_metrics_counts_only_today_closed_live_trades(tmp_path):
    from daily_metrics import calculate_daily_metrics
    from journal import append_journal_event

    journal = tmp_path / "trade_journal.jsonl"
    append_journal_event(journal, event_type="exit", coin="BTC", side="long", price=100, size=1, dry_run=False, realized_pnl_usd=10.5, ts=datetime(2026, 5, 28, 8, 0, tzinfo=timezone.utc))
    append_journal_event(journal, event_type="exit", coin="ETH", side="long", price=100, size=1, dry_run=False, realized_pnl_usd=-4.0, ts=datetime(2026, 5, 28, 9, 0, tzinfo=timezone.utc))
    append_journal_event(journal, event_type="exit", coin="SOL", side="long", price=100, size=1, dry_run=True, realized_pnl_usd=999.0, ts=datetime(2026, 5, 28, 10, 0, tzinfo=timezone.utc))
    append_journal_event(journal, event_type="exit", coin="XRP", side="long", price=100, size=1, dry_run=False, realized_pnl_usd=99.0, ts=datetime(2026, 5, 27, 23, 59, tzinfo=timezone.utc))

    metrics = calculate_daily_metrics(journal, now=datetime(2026, 5, 28, 12, 0, tzinfo=timezone.utc))

    assert metrics.date == "2026-05-28"
    assert metrics.closed_trade_count == 2
    assert metrics.realized_pnl_usd == 6.5


def test_daily_metrics_returns_zero_for_missing_journal(tmp_path):
    from daily_metrics import calculate_daily_metrics

    metrics = calculate_daily_metrics(tmp_path / "missing.jsonl", now=datetime(2026, 5, 28, 12, 0, tzinfo=timezone.utc))

    assert metrics.closed_trade_count == 0
    assert metrics.realized_pnl_usd == 0.0


def test_alert_plan_formats_entry_without_side_effects():
    from alerting import build_trade_alert

    alert = build_trade_alert(event_type="entry", coin="BTC", side="long", price=100.0, size=0.25, dry_run=True, reason="flash_crash")

    assert alert.destination_topic == "Crypto Trading Bot"
    assert alert.side_effects == ()
    assert "ENTRY" in alert.text
    assert "BTC" in alert.text
    assert "DRY-RUN" in alert.text
    assert "flash_crash" in alert.text


def test_alert_plan_formats_risk_block_without_credentials_or_amount_leak():
    from alerting import build_risk_alert

    alert = build_risk_alert(reason="daily_loss_limit", dry_run=False)

    assert alert.side_effects == ()
    assert "Risk Gate" in alert.text
    assert "daily_loss_limit" in alert.text
    assert "HL_API_PRIVATE_KEY" not in alert.text
