"""study_concepts API — 이론공부 홈(오늘의 개념 · 진도 · 회독 SR). prefix = /api/study.

문제풀이 표면 무접촉. 개념문서(가스기사 태그) 읽기 집계 + 회독 SR write 만. 단일 토픽(가스기사=4).
경로: GET /curriculum · GET /today-concepts · POST /concepts/{doc_id}/read.
"""

from __future__ import annotations

from typing import Annotated

from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.ext.asyncio import AsyncSession

from core.auth import get_current_user
from core.database import get_session
from models.user import User
from services.study import concept_curriculum as cc
from services.study import concept_links as cl

router = APIRouter()

# 가스기사 단일 토픽 운영(현행). 다토픽 확장 시 쿼리 파라미터로 승격.
DEFAULT_TOPIC_ID = 4


@router.get("/curriculum")
async def get_curriculum(
    user: Annotated[User, Depends(get_current_user)],
    session: Annotated[AsyncSession, Depends(get_session)],
    topic_id: int = DEFAULT_TOPIC_ID,
):
    """과목별 회독 진도 + 개념/문항 복습 due 요약."""
    return await cc.curriculum(session, user.id, topic_id)


@router.get("/today-concepts")
async def get_today_concepts(
    user: Annotated[User, Depends(get_current_user)],
    session: Annotated[AsyncSession, Depends(get_session)],
    topic_id: int = DEFAULT_TOPIC_ID,
    limit: int = 6,
):
    """오늘 공부할 개념(재복습 → 미독 빈출순)."""
    return await cc.today_concepts(session, user.id, topic_id, limit)


@router.get("/concepts/weakness-map")
async def get_weakness_map(
    user: Annotated[User, Depends(get_current_user)],
    session: Annotated[AsyncSession, Depends(get_session)],
    topic_id: int = DEFAULT_TOPIC_ID,
    limit: int = 12,
):
    """개념 약점 지도 — 링크된 기출 정답률로 약점 개념(정답률<60%) 우선(이론↔문제)."""
    name = await cc._topic_name(session, topic_id)
    if not name:
        return {"weak": [], "weak_total": 0, "evaluated_total": 0}
    return await cl.weakness_map(session, user.id, name, limit)


@router.get("/concepts/{doc_id}")
async def get_concept_detail(
    doc_id: int,
    user: Annotated[User, Depends(get_current_user)],
    session: Annotated[AsyncSession, Depends(get_session)],
    topic_id: int = DEFAULT_TOPIC_ID,
):
    """개념 리더 재료 — 구조 파싱(요약/본문/빈출/관련) + 백링크 해소 + 회독/SR + 이전/다음."""
    detail = await cc.concept_detail(session, user.id, topic_id, doc_id)
    if detail is None:
        raise HTTPException(status_code=404, detail="concept not found")
    return detail


@router.get("/concepts/{doc_id}/questions")
async def get_concept_questions(
    doc_id: int,
    user: Annotated[User, Depends(get_current_user)],
    session: Annotated[AsyncSession, Depends(get_session)],
    limit: int = 20,
):
    """개념 관련 기출 + 내 정답률 (이론↔문제 브리지)."""
    return await cl.related_questions(session, user.id, doc_id, limit)


@router.post("/concepts/{doc_id}/read")
async def post_concept_read(
    doc_id: int,
    user: Annotated[User, Depends(get_current_user)],
    session: Annotated[AsyncSession, Depends(get_session)],
    topic_id: int = DEFAULT_TOPIC_ID,
):
    """개념 회독 처리 → 회독 플래그 + SR 입고/전진."""
    return await cc.mark_read(session, user.id, topic_id, doc_id)
