# -*- coding: utf-8 -*-
"""投资人端端点测试：投资方向/项目/意向/路演审核规则。"""
from __future__ import annotations

from fastapi.testclient import TestClient


def login(client: TestClient, username: str, password: str = "123456") -> str:
    res = client.post("/auth/login", json={"username": username, "password": password})
    assert res.status_code == 200, res.text
    return res.json()["token"]


def auth(token: str) -> dict:
    return {"Authorization": f"Bearer {token}"}


def test_investor_pages(client):
    token = login(client, "inv01")
    for p in ["/investor/dashboard", "/investor/preferences", "/investor/projects",
              "/investor/trainings", "/investor/portfolio", "/investor/roadshows", "/investor/intents"]:
        res = client.get(p, headers=auth(token))
        assert res.status_code == 200, f"{p}: {res.text}"


def test_investor_preferences_update(client):
    token = login(client, "inv01")
    res = client.put("/investor/preferences", headers=auth(token),
                     json={"industries": ["人工智能"], "stage": "seed", "amount_min": 50, "amount_max": 200})
    assert res.status_code == 200, res.text
    assert res.json()["industries"] == ["人工智能"]


def test_roadshow_review_rules(client):
    # 投资人发布 → 需审核（submitted）
    inv = login(client, "inv01")
    res = client.post("/investor/roadshows", headers=auth(inv),
                      json={"title": "投资人专场", "activity_type": "online", "scope_type": "all",
                            "start_at": "2026-09-01T10:00:00", "end_at": "2026-09-01T12:00:00"})
    assert res.status_code == 200, res.text
    assert res.json()["need_review"] is True
    assert res.json()["status"] == "submitted"

    # 政务在自身权限范围内发布 → 免审（published）；跨范围 → 需审
    gov = login(client, "gov_prov")  # 省级，scope 全省
    res2 = client.post("/investor/roadshows", headers=auth(gov),
                       json={"title": "省级对接会", "activity_type": "hybrid", "scope_type": "region",
                             "region_id": "r_prov_yn",
                             "start_at": "2026-09-02T10:00:00", "end_at": "2026-09-02T12:00:00"})
    assert res2.status_code == 200, res2.text
    assert res2.json()["need_review"] is False
    assert res2.json()["status"] == "published"


def test_investor_rejects_other_role(client):
    token = login(client, "opc01")
    res = client.get("/investor/projects", headers=auth(token))
    assert res.status_code == 403
