"""
쿼리 이력·즐겨찾기·공유 대시보드

운영자가 자주 쓰는 쿼리를 저장하고 팀과 공유.

엔드포인트:
  GET  /api/queryhistory/             — 내 쿼리 이력
  GET  /api/queryhistory/favorites    — 즐겨찾기
  POST /api/queryhistory/{id}/favorite — 즐겨찾기 토글
  POST /api/queryhistory/save         — 쿼리 직접 저장
  GET  /api/queryhistory/shared       — 팀 공유 쿼리
  POST /api/queryhistory/{id}/share   — 공유 토글
  DELETE /api/queryhistory/{id}       — 이력 삭제
"""
from __future__ import annotations

from datetime import datetime
from typing import Optional

from fastapi import APIRouter, Depends, HTTPException
from pydantic import BaseModel
from sqlalchemy import select, desc
from sqlalchemy.ext.asyncio import AsyncSession

from core.auth import get_current_user
from database import get_db
from models import User, QueryHistory

router = APIRouter(prefix="/api/queryhistory", tags=["쿼리 이력"])


class SaveQueryRequest(BaseModel):
    question: str
    sql: str
    description: Optional[str] = None


@router.get("/")
async def list_history(
    limit: int = 50,
    db: AsyncSession = Depends(get_db),
    user: User = Depends(get_current_user),
):
    rows = await db.execute(
        select(QueryHistory).where(QueryHistory.user_id == user.id)
        .order_by(desc(QueryHistory.executed_at)).limit(limit)
    )
    hs = rows.scalars().all()
    return [
        {"id": h.id, "question": h.question, "sql": h.generated_sql,
         "rows": h.row_count, "is_favorite": h.is_favorite,
         "is_shared": h.is_shared, "executed_at": h.executed_at}
        for h in hs
    ]


@router.get("/favorites")
async def list_favorites(
    db: AsyncSession = Depends(get_db),
    user: User = Depends(get_current_user),
):
    rows = await db.execute(
        select(QueryHistory).where(
            QueryHistory.user_id == user.id,
            QueryHistory.is_favorite == True,
        ).order_by(desc(QueryHistory.executed_at))
    )
    hs = rows.scalars().all()
    return [
        {"id": h.id, "question": h.question, "sql": h.generated_sql,
         "description": h.description, "executed_at": h.executed_at}
        for h in hs
    ]


@router.get("/shared")
async def list_shared(
    db: AsyncSession = Depends(get_db),
    user: User = Depends(get_current_user),
):
    """팀 공유 쿼리 (모든 테넌트 사용자)."""
    rows = await db.execute(
        select(QueryHistory, User.name.label("owner")).join(
            User, QueryHistory.user_id == User.id
        ).where(
            QueryHistory.is_shared == True,
        ).order_by(desc(QueryHistory.executed_at)).limit(100)
    )
    return [
        {"id": r.QueryHistory.id, "question": r.QueryHistory.question,
         "sql": r.QueryHistory.generated_sql, "description": r.QueryHistory.description,
         "owner": r.owner, "executed_at": r.QueryHistory.executed_at}
        for r in rows.all()
    ]


@router.post("/save")
async def save_query(
    req: SaveQueryRequest,
    db: AsyncSession = Depends(get_db),
    user: User = Depends(get_current_user),
):
    h = QueryHistory(
        user_id=user.id,
        question=req.question,
        generated_sql=req.sql,
        description=req.description,
        row_count=0,
        is_favorite=True,
        executed_at=datetime.utcnow(),
    )
    db.add(h)
    await db.commit()
    await db.refresh(h)
    return {"ok": True, "id": h.id}


@router.post("/{history_id}/favorite")
async def toggle_favorite(
    history_id: int,
    db: AsyncSession = Depends(get_db),
    user: User = Depends(get_current_user),
):
    row = await db.execute(
        select(QueryHistory).where(
            QueryHistory.id == history_id,
            QueryHistory.user_id == user.id,
        )
    )
    h = row.scalar_one_or_none()
    if not h:
        raise HTTPException(404)
    h.is_favorite = not h.is_favorite
    await db.commit()
    return {"ok": True, "is_favorite": h.is_favorite}


@router.post("/{history_id}/share")
async def toggle_share(
    history_id: int,
    db: AsyncSession = Depends(get_db),
    user: User = Depends(get_current_user),
):
    row = await db.execute(
        select(QueryHistory).where(
            QueryHistory.id == history_id,
            QueryHistory.user_id == user.id,
        )
    )
    h = row.scalar_one_or_none()
    if not h:
        raise HTTPException(404)
    h.is_shared = not h.is_shared
    await db.commit()
    return {"ok": True, "is_shared": h.is_shared}


@router.delete("/{history_id}")
async def delete_history(
    history_id: int,
    db: AsyncSession = Depends(get_db),
    user: User = Depends(get_current_user),
):
    row = await db.execute(
        select(QueryHistory).where(
            QueryHistory.id == history_id,
            QueryHistory.user_id == user.id,
        )
    )
    h = row.scalar_one_or_none()
    if not h:
        raise HTTPException(404)
    await db.delete(h)
    await db.commit()
    return {"ok": True}
