"""战略地图版本管理 API"""
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from app.database import get_db
from app.deps import get_entity_id
from app.auth_middleware import require_role
from app.models import StrategicMap, StrategicMapVersion

router = APIRouter(prefix="/api/cma/maps", tags=["战略地图版本"],
    dependencies=[Depends(require_role("ceo", "finance"))],
)


@router.get("/{map_id}/versions")
def list_versions(map_id: int, db: Session = Depends(get_db), entity_id: int = Depends(get_entity_id)):
    """查看版本历史（账套隔离, OpenCode审查#9）"""
    versions = db.query(StrategicMapVersion).filter(
        StrategicMapVersion.map_id == map_id,
        StrategicMapVersion.entity_id == entity_id,
    ).order_by(StrategicMapVersion.id.desc()).all()
    return {"data": [v_to_dict(v) for v in versions]}


@router.post("/{map_id}/versions/snapshot")
def create_snapshot(map_id: int, data: dict, db: Session = Depends(get_db), entity_id: int = Depends(get_entity_id)):
    """手动创建快照（账套隔离）"""
    m = db.query(StrategicMap).filter(StrategicMap.id == map_id, StrategicMap.entity_id == entity_id).first()
    if not m:
        raise HTTPException(404, "战略地图不存在")

    import json
    dims = m.dimensions
    canvas = m.canvas_data
    if isinstance(dims, str):
        dims = json.loads(dims)
    if isinstance(canvas, str):
        canvas = json.loads(canvas)

    # 自动版本号
    existing = db.query(StrategicMapVersion).filter(
        StrategicMapVersion.map_id == map_id
    ).order_by(StrategicMapVersion.id.desc()).first()
    if existing:
        import re
        match = re.search(r"v(\d+)\.(\d+)", existing.version)
        major = int(match.group(1)) if match else 1
        minor = int(match.group(2)) + 1 if match else 0
        new_ver = f"v{major}.{minor}"
    else:
        new_ver = "v1.0"

    snapshot = StrategicMapVersion(
        map_id=map_id,
        entity_id=entity_id,  # 继承地图企业
        version=new_ver,
        dimensions=dims,
        canvas_data=canvas,
        comment=data.get("comment", f"手动快照 {new_ver}"),
    )
    db.add(snapshot)
    db.commit()
    db.refresh(snapshot)
    return v_to_dict(snapshot)


@router.post("/{map_id}/versions/{ver_id}/rollback")
def rollback_version(map_id: int, ver_id: int, db: Session = Depends(get_db), entity_id: int = Depends(get_entity_id)):
    """回滚到指定版本"""
    m = db.query(StrategicMap).filter(StrategicMap.id == map_id, StrategicMap.entity_id == entity_id).first()
    if not m:
        raise HTTPException(404, "战略地图不存在")

    v = db.query(StrategicMapVersion).filter(
        StrategicMapVersion.id == ver_id,
        StrategicMapVersion.map_id == map_id,
    ).first()
    if not v:
        raise HTTPException(404, "版本不存在")

    m.dimensions = v.dimensions
    m.canvas_data = v.canvas_data
    m.version = f"rollback-{v.version}"
    m.status = "draft"
    db.commit()
    return {"message": f"已回滚到 {v.version}", "version": m.version}


def v_to_dict(v):
    return {c.name: getattr(v, c.name) for c in v.__table__.columns}
