from fastapi import APIRouter, HTTPException, Request
from pydantic import BaseModel
from services.report_service import generate_daily_report
from datetime import date

router = APIRouter(tags=["daily_report"])


class DailyReportRequest(BaseModel):
    date: str | None = None
    project_id: int | None = None


@router.post("/report/daily")
async def daily_report(req: DailyReportRequest, request: Request):
    report_date = req.date or date.today().isoformat()
    token = request.headers.get("authorization", "").replace("Bearer ", "")
    try:
        result = await generate_daily_report(report_date, req.project_id, token)
        return {"available": True, **result}
    except Exception as e:
        raise HTTPException(status_code=500, detail="AI 서비스 처리 중 오류가 발생했습니다")


@router.post("/report/preview")
async def report_preview(req: DailyReportRequest, request: Request):
    report_date = req.date or date.today().isoformat()
    token = request.headers.get("authorization", "").replace("Bearer ", "")
    try:
        result = await generate_daily_report(report_date, req.project_id, token)
        return {"available": True, "preview": True, **result}
    except Exception as e:
        raise HTTPException(status_code=500, detail="AI 서비스 처리 중 오류가 발생했습니다")
