from typing import Optional
from fastapi import APIRouter, Depends, Query
from sqlalchemy.orm import Session

from app.core.database import get_db
from app.models.user import User
from app.schemas.common import ResponseModel, ListResponse
from app.api.deps import get_current_user

router = APIRouter()


@router.get("/my-tasks")
def my_tasks(
    status: Optional[str] = Query(None),
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_user),
):
    from app.models.project import ClassificationTask
    query = db.query(ClassificationTask).filter(ClassificationTask.assignee_id == current_user.id)
    if status:
        query = query.filter(ClassificationTask.status == status)
    items = query.order_by(ClassificationTask.created_at.desc()).all()
    data = []
    for t in items:
        data.append({
            "id": t.id,
            "name": t.name,
            "project_id": t.project_id,
            "status": t.status,
            "deadline": t.deadline.isoformat() if t.deadline else None,
            "created_at": t.created_at.isoformat() if t.created_at else None,
        })
    return ResponseModel(data=data)


@router.get("/my-tasks/{task_id}/items")
def task_items(
    task_id: int,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_user),
):
    from app.models.project import ClassificationTask, ClassificationResult
    from app.models.metadata import DataColumn, DataTable, Database as MetaDatabase, DataSource
    from app.models.classification import Category, DataLevel

    task = db.query(ClassificationTask).filter(ClassificationTask.id == task_id).first()
    if not task:
        from fastapi import HTTPException, status
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="任务不存在")

    results = db.query(ClassificationResult).filter(
        ClassificationResult.project_id == task.project_id,
    ).join(DataColumn).all()

    data = []
    for r in results:
        col = r.column
        table = col.table if col else None
        database = table.database if table else None
        source = database.source if database else None
        data.append({
            "result_id": r.id,
            "column_id": col.id if col else None,
            "column_name": col.name if col else None,
            "data_type": col.data_type if col else None,
            "comment": col.comment if col else None,
            "table_name": table.name if table else None,
            "database_name": database.name if database else None,
            "source_name": source.name if source else None,
            "category_id": r.category_id,
            "category_name": r.category.name if r.category else None,
            "level_id": r.level_id,
            "level_name": r.level.name if r.level else None,
            "level_color": r.level.color if r.level else None,
            "source": r.source,
            "confidence": r.confidence,
            "status": r.status,
        })
    return ResponseModel(data=data)
