"""
战略地图模块测试
"""
import pytest
from fastapi.testclient import TestClient
from sqlalchemy.orm import Session
from tests.conftest import create_test_user, get_token_for_user, auth_header


class TestMaps:
    """战略地图CRUD测试"""

    def test_list_maps_empty(self, client: TestClient, db: Session):
        """空列表"""
        create_test_user(db)
        token = get_token_for_user(client)

        resp = client.get("/api/cma/maps", headers=auth_header(token))
        assert resp.status_code == 200
        assert resp.json()["data"] == []

    def test_create_with_template(self, client: TestClient, db: Session):
        """创建带模板的地图"""
        create_test_user(db)
        token = get_token_for_user(client)

        resp = client.post(
            "/api/cma/maps/create-with-template",
            headers=auth_header(token),
            json={"title": "测试模板地图"},
        )
        assert resp.status_code == 200
        data = resp.json()
        assert data["title"] == "测试模板地图"
        assert data["status"] == "draft"
        assert len(data["dimensions"]) == 4
        assert len(data["dimensions"][0]["objectives"]) > 0

    def test_update_map(self, client: TestClient, db: Session):
        """编辑地图"""
        create_test_user(db)
        token = get_token_for_user(client)

        create_resp = client.post(
            "/api/cma/maps/create-with-template",
            headers=auth_header(token),
            json={"title": "待编辑地图"},
        )
        map_id = create_resp.json()["id"]

        update_resp = client.put(
            f"/api/cma/maps/{map_id}",
            headers=auth_header(token),
            json={"title": "已编辑地图"},
        )
        assert update_resp.status_code == 200
        assert update_resp.json()["title"] == "已编辑地图"

    def test_publish_map_creates_version(self, client: TestClient, db: Session):
        """发布地图触发版本快照"""
        create_test_user(db)
        token = get_token_for_user(client)

        create_resp = client.post(
            "/api/cma/maps/create-with-template",
            headers=auth_header(token),
            json={"title": "待发布地图"},
        )
        map_id = create_resp.json()["id"]

        # 发布
        client.put(
            f"/api/cma/maps/{map_id}",
            headers=auth_header(token),
            json={"status": "published"},
        )

        ver_resp = client.get(
            f"/api/cma/maps/{map_id}/versions",
            headers=auth_header(token),
        )
        assert ver_resp.status_code == 200
        versions = ver_resp.json()["data"]
        assert len(versions) >= 1
        assert versions[0]["version"] == "v1.0"

    def test_add_connection(self, client: TestClient, db: Session):
        """添加因果连线"""
        create_test_user(db)
        token = get_token_for_user(client)

        create_resp = client.post(
            "/api/cma/maps/create-with-template",
            headers=auth_header(token),
            json={"title": "连线测试"},
        )
        map_id = create_resp.json()["id"]

        resp = client.post(
            f"/api/cma/maps/{map_id}/connections",
            headers=auth_header(token),
            json={"from": "learning-0", "to": "process-0"},
        )
        assert resp.status_code == 200
        assert len(resp.json()["connections"]) == 1

    def test_same_dim_connection_fails(self, client: TestClient, db: Session):
        """同维度连线被拒绝"""
        create_test_user(db)
        token = get_token_for_user(client)

        create_resp = client.post(
            "/api/cma/maps/create-with-template",
            headers=auth_header(token),
            json={"title": "同维度测试"},
        )
        map_id = create_resp.json()["id"]

        resp = client.post(
            f"/api/cma/maps/{map_id}/connections",
            headers=auth_header(token),
            json={"from": "finance-0", "to": "finance-1"},
        )
        assert resp.status_code == 400
        assert "不能" in resp.json()["detail"]
