import httpx
import os
from fastapi import HTTPException, Request

TKUSER_API_URL = os.getenv("TKUSER_API_URL", "http://tkuser-api:3000")


def get_token_from_request(request: Request) -> str:
    """Request 헤더에서 Bearer 토큰 추출"""
    auth = request.headers.get("Authorization", "")
    if auth.startswith("Bearer "):
        return auth[7:]
    return request.cookies.get("sso_token", "")


def _headers(token: str) -> dict:
    if not token:
        raise HTTPException(status_code=401, detail="인증 토큰이 필요합니다.")
    return {"Authorization": f"Bearer {token}"}


def _map_project(data: dict) -> dict:
    """tkuser API 응답을 S3 프론트엔드 형식으로 매핑 (project_id → id)"""
    return {
        "id": data.get("project_id"),
        "job_no": data.get("job_no"),
        "project_name": data.get("project_name"),
        "is_active": data.get("is_active", True),
        "created_at": data.get("created_at"),
    }


async def get_projects(token: str, active_only: bool = True) -> list:
    """프로젝트 목록 조회"""
    endpoint = "/api/projects/active" if active_only else "/api/projects"
    async with httpx.AsyncClient(timeout=10.0) as client:
        resp = await client.get(f"{TKUSER_API_URL}{endpoint}", headers=_headers(token))
        if resp.status_code != 200:
            raise HTTPException(status_code=resp.status_code, detail="프로젝트 목록 조회 실패")
        body = resp.json()
        if not body.get("success"):
            raise HTTPException(status_code=500, detail=body.get("error", "알 수 없는 오류"))
        return [_map_project(p) for p in body.get("data", [])]


async def get_project(token: str, project_id: int) -> dict | None:
    """특정 프로젝트 조회. 없으면 None 반환"""
    async with httpx.AsyncClient(timeout=10.0) as client:
        resp = await client.get(
            f"{TKUSER_API_URL}/api/projects/{project_id}", headers=_headers(token)
        )
        if resp.status_code == 404:
            return None
        if resp.status_code != 200:
            raise HTTPException(status_code=resp.status_code, detail="프로젝트 조회 실패")
        body = resp.json()
        if not body.get("success"):
            raise HTTPException(status_code=500, detail=body.get("error", "알 수 없는 오류"))
        return _map_project(body.get("data"))


async def create_project(token: str, data: dict) -> dict:
    """프로젝트 생성"""
    async with httpx.AsyncClient(timeout=10.0) as client:
        resp = await client.post(
            f"{TKUSER_API_URL}/api/projects", headers=_headers(token), json=data
        )
        if resp.status_code == 409:
            raise HTTPException(status_code=400, detail="이미 존재하는 Job No.입니다.")
        if resp.status_code not in (200, 201):
            raise HTTPException(status_code=resp.status_code, detail="프로젝트 생성 실패")
        body = resp.json()
        if not body.get("success"):
            raise HTTPException(status_code=500, detail=body.get("error", "알 수 없는 오류"))
        return _map_project(body.get("data"))


async def update_project(token: str, project_id: int, data: dict) -> dict:
    """프로젝트 수정"""
    async with httpx.AsyncClient(timeout=10.0) as client:
        resp = await client.put(
            f"{TKUSER_API_URL}/api/projects/{project_id}",
            headers=_headers(token),
            json=data,
        )
        if resp.status_code == 404:
            raise HTTPException(status_code=404, detail="프로젝트를 찾을 수 없습니다.")
        if resp.status_code != 200:
            raise HTTPException(status_code=resp.status_code, detail="프로젝트 수정 실패")
        body = resp.json()
        if not body.get("success"):
            raise HTTPException(status_code=500, detail=body.get("error", "알 수 없는 오류"))
        return _map_project(body.get("data"))


async def delete_project(token: str, project_id: int) -> dict:
    """프로젝트 삭제 (비활성화)"""
    async with httpx.AsyncClient(timeout=10.0) as client:
        resp = await client.delete(
            f"{TKUSER_API_URL}/api/projects/{project_id}", headers=_headers(token)
        )
        if resp.status_code == 404:
            raise HTTPException(status_code=404, detail="프로젝트를 찾을 수 없습니다.")
        if resp.status_code != 200:
            raise HTTPException(status_code=resp.status_code, detail="프로젝트 삭제 실패")
        return resp.json()
