# app/web/admin_matches.py
from fastapi import APIRouter
from app.services.supabase_service import get_client
sb = get_client()
router = APIRouter(prefix="/admin/matches", tags=["admin-matches"])

@router.get("/pending")
def list_pending(batch_id: str):
    pending = sb.table("govtrack.match_decisions").select("*")\
        .eq("batch_id", batch_id).eq("decision","PENDING").execute().data or []
    # 후보 탑3 같이 반환
    out = []
    for d in pending:
        cands = sb.table("govtrack.match_candidates").select("*")\
            .eq("batch_id", batch_id).eq("staging_id", d["staging_id"])\
            .order("score", desc=True).limit(3).execute().data or []
        out.append({"decision": d, "candidates": cands})
    return out

@router.post("/resolve")
def solve_pending(batch_id: str, staging_id: int, person_id: str):
    # 기존 PENDING을 MANUAL로 바꾸고 person_id 채움
    sb.table("govtrack.match_decisions").update({
        "person_id": person_id, "decision": "MANUAL", "rationale": "admin set"
    }).eq("batch_id", batch_id).eq("staging_id", staging_id).execute()
    return {"ok": True}
