# 온보딩 진행 상태 API — user_profile_summary 단일 테이블 기반
from fastapi import APIRouter
from app.core.config import A_DB_CONFIG, logger
import mysql.connector

router = APIRouter(
    prefix="",
    tags=["Onboarding"]
)

@router.get("/progress/{session_id}")
async def get_onboarding_progress(session_id: str):
    """
    user_profile_summary의 각 컬럼 null 체크로 완료 여부 판단.
    """
    conn = None
    try:
        conn = mysql.connector.connect(**A_DB_CONFIG)
        cursor = conn.cursor(dictionary=True)

        cursor.execute(
            """
            SELECT 
                username,
                final_goal,
                mbti,
                spi_language,
                it_level
            FROM user_profile_summary
            WHERE session_id = %s
            """,
            (session_id,)
        )
        row = cursor.fetchone()
        cursor.close()
        conn.close()

        if not row:
            return {
                "username": None,
                "goal_completed": False,
                "mbti_completed": False,
                "spi_completed": False,
                "it_completed": False,
            }

        return {
            "username": row.get("username"),
            "goal_completed": row.get("final_goal") is not None,
            "mbti_completed": row.get("mbti") is not None,
            "spi_completed": row.get("spi_language") is not None,
            "it_completed": row.get("it_level") is not None,
        }

    except Exception as e:
        logger.error(f"進行状況取得エラー: {e}")
        if conn:
            conn.close()
        return {
            "username": None,
            "goal_completed": False,
            "mbti_completed": False,
            "spi_completed": False,
            "it_completed": False,
        }
