from fastapi import APIRouter, Depends, HTTPException, Request, status
from database.models import User, UserRole
from routers.auth import get_current_user
from utils.tkuser_client import get_token_from_request
import utils.tkuser_client as tkuser_client

router = APIRouter(
    prefix="/api/projects",
    tags=["projects"]
)

@router.options("/")
async def projects_options():
    """OPTIONS preflight 요청 처리"""
    return {"message": "OK"}

@router.get("/")
async def get_projects(
    request: Request,
    skip: int = 0,
    limit: int = 100,
    active_only: bool = True,
):
    """프로젝트 목록 조회 - tkuser API로 프록시"""
    token = get_token_from_request(request)
    projects = await tkuser_client.get_projects(token, active_only=active_only)
    return projects[skip:skip + limit]

@router.get("/{project_id}")
async def get_project(
    project_id: int,
    request: Request,
):
    """특정 프로젝트 조회 - tkuser API로 프록시"""
    token = get_token_from_request(request)
    project = await tkuser_client.get_project(token, project_id)
    if not project:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="프로젝트를 찾을 수 없습니다."
        )
    return project
