"""API endpoint tests using FastAPI TestClient.

These test the deterministic endpoints (profile, config, health, track-batch).
LLM endpoints are not tested here (they need a live Gemini key).
"""
import sys
import os

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from fastapi.testclient import TestClient
from app import app

client = TestClient(app)


class TestHealthEndpoint:
    def test_health_returns_json(self):
        r = client.get("/api/health")
        assert r.status_code == 200
        data = r.json()
        assert "status" in data
        assert "db" in data


class TestConfigEndpoint:
    def test_config_returns_urls(self):
        r = client.get("/api/config")
        assert r.status_code == 200
        data = r.json()
        assert "subscription_url" in data
        assert "stripe_url" in data


class TestProfileEndpoint:
    def test_basic_profile(self):
        r = client.post("/api/profile", json={
            "cigs_per_day": 10,
            "first_cig_minutes": 30,
            "readiness": 7,
            "likes_about_smoking": ["relax"],
            "triggers": ["stress", "work"],
        })
        assert r.status_code == 200
        data = r.json()
        assert data["smoker_type"] in {"stress", "social", "habitual", "emotional", "reward", "identity"}
        assert 65 <= data["success_probability"] <= 94
        assert data["program"] == "six_day"
        assert "quit_date" in data
        assert "savings" in data

    def test_heavy_smoker(self):
        r = client.post("/api/profile", json={
            "cigs_per_day": 30,
            "first_cig_minutes": 5,
            "readiness": 3,
            "past_attempts_count": 5,
            "likes_about_smoking": ["relax", "stress"],
            "triggers": ["morning", "coffee", "meals", "work", "evening", "stress"],
        })
        assert r.status_code == 200
        data = r.json()
        assert data["success_probability"] == 65

    def test_light_smoker(self):
        r = client.post("/api/profile", json={
            "cigs_per_day": 3,
            "first_cig_minutes": 120,
            "readiness": 9,
            "past_attempts_count": 0,
            "likes_about_smoking": ["social"],
            "triggers": ["social", "alcohol"],
        })
        assert r.status_code == 200
        data = r.json()
        assert data["success_probability"] >= 80

    def test_empty_body(self):
        r = client.post("/api/profile", json={})
        assert r.status_code == 200

    def test_invalid_json(self):
        r = client.post("/api/profile", content=b"not json", headers={"content-type": "application/json"})
        assert r.status_code == 400

    def test_vaper_savings(self):
        r = client.post("/api/profile", json={
            "cigs_per_day": 0,
            "vaper_weekly_cost": 70,
            "readiness": 7,
        })
        data = r.json()
        assert data["savings"]["monthly"] == 300


class TestTrackBatchEndpoint:
    def test_missing_data_returns_400(self):
        r = client.post("/api/track-batch", json={})
        assert r.status_code == 400

    def test_missing_events_returns_400(self):
        r = client.post("/api/track-batch", json={"sessionId": "abc"})
        assert r.status_code == 400

    def test_missing_session_returns_400(self):
        r = client.post("/api/track-batch", json={"events": [{"step": "test"}]})
        assert r.status_code == 400


class TestIndexPage:
    def test_serves_html(self):
        r = client.get("/")
        assert r.status_code == 200
        assert "text/html" in r.headers["content-type"]
