import os
import logging
from contextlib import asynccontextmanager
from fastapi import FastAPI, Depends, HTTPException
from fastapi.middleware.cors import CORSMiddleware

from .db import (
    init_db, get_public_data,
    get_profile, update_profile,
    get_careers, create_career, update_career, delete_career,
    get_projects, create_project, update_project, delete_project,
    get_skills, create_skill, update_skill, delete_skill,
    get_introductions, create_introduction, update_introduction,
    delete_introduction, set_main_introduction,
    get_all_todos, create_todo, update_todo, delete_todo, delete_done_todos,
    get_all_posts, create_post, update_post, delete_post,
)
from .models import (
    ProfileUpdate, CareerCreate, CareerUpdate,
    ProjectCreate, ProjectUpdate, SkillCreate, SkillUpdate,
    IntroCreate, IntroUpdate, AuthRequest,
    TodoCreate, TodoUpdate, BlogPostCreate, BlogPostUpdate,
)
from .auth import authenticate, require_auth

logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(name)s] %(levelname)s %(message)s")
logger = logging.getLogger("personal")


@asynccontextmanager
async def lifespan(app: FastAPI):
    init_db()
    logger.info("personal service 시작")
    yield


app = FastAPI(lifespan=lifespan)

_cors_origins = os.getenv("CORS_ALLOW_ORIGINS", "http://localhost:3007,http://localhost:8080").split(",")
app.add_middleware(
    CORSMiddleware,
    allow_origins=[o.strip() for o in _cors_origins],
    allow_credentials=False,
    allow_methods=["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"],
    allow_headers=["Content-Type", "Authorization"],
)


@app.get("/health")
def health():
    return {"status": "ok"}


# ── Public ──

@app.get("/api/profile/public")
def api_public():
    return get_public_data()


# ── Auth ──

@app.post("/api/profile/auth")
def api_auth(body: AuthRequest):
    return authenticate(body.password)


# ── Profile (편집) ──

@app.get("/api/profile/profile", dependencies=[Depends(require_auth)])
def api_profile_get():
    return get_profile()


@app.put("/api/profile/profile", dependencies=[Depends(require_auth)])
def api_profile_update(body: ProfileUpdate):
    return update_profile(body.model_dump(exclude_none=True))


# ── Careers (편집) ──

@app.get("/api/profile/careers", dependencies=[Depends(require_auth)])
def api_careers_list():
    return get_careers()


@app.post("/api/profile/careers", status_code=201, dependencies=[Depends(require_auth)])
def api_career_create(body: CareerCreate):
    return create_career(body.model_dump())


@app.put("/api/profile/careers/{career_id}", dependencies=[Depends(require_auth)])
def api_career_update(career_id: int, body: CareerUpdate):
    result = update_career(career_id, body.model_dump(exclude_none=True))
    if not result:
        raise HTTPException(status_code=404, detail="Career not found")
    return result


@app.delete("/api/profile/careers/{career_id}", dependencies=[Depends(require_auth)])
def api_career_delete(career_id: int):
    if not delete_career(career_id):
        raise HTTPException(status_code=404, detail="Career not found")
    return {"ok": True}


# ── Projects (편집) ──

@app.get("/api/profile/projects", dependencies=[Depends(require_auth)])
def api_projects_list():
    return get_projects()


@app.post("/api/profile/projects", status_code=201, dependencies=[Depends(require_auth)])
def api_project_create(body: ProjectCreate):
    return create_project(body.model_dump())


@app.put("/api/profile/projects/{project_id}", dependencies=[Depends(require_auth)])
def api_project_update(project_id: int, body: ProjectUpdate):
    result = update_project(project_id, body.model_dump(exclude_none=True))
    if not result:
        raise HTTPException(status_code=404, detail="Project not found")
    return result


@app.delete("/api/profile/projects/{project_id}", dependencies=[Depends(require_auth)])
def api_project_delete(project_id: int):
    if not delete_project(project_id):
        raise HTTPException(status_code=404, detail="Project not found")
    return {"ok": True}


# ── Skills (편집) ──

@app.get("/api/profile/skills", dependencies=[Depends(require_auth)])
def api_skills_list():
    return get_skills()


@app.post("/api/profile/skills", status_code=201, dependencies=[Depends(require_auth)])
def api_skill_create(body: SkillCreate):
    return create_skill(body.model_dump())


@app.put("/api/profile/skills/{skill_id}", dependencies=[Depends(require_auth)])
def api_skill_update(skill_id: int, body: SkillUpdate):
    result = update_skill(skill_id, body.model_dump(exclude_none=True))
    if not result:
        raise HTTPException(status_code=404, detail="Skill not found")
    return result


@app.delete("/api/profile/skills/{skill_id}", dependencies=[Depends(require_auth)])
def api_skill_delete(skill_id: int):
    if not delete_skill(skill_id):
        raise HTTPException(status_code=404, detail="Skill not found")
    return {"ok": True}


# ── Introductions (편집) ──

@app.get("/api/profile/introductions", dependencies=[Depends(require_auth)])
def api_intros_list():
    return get_introductions()


@app.post("/api/profile/introductions", status_code=201, dependencies=[Depends(require_auth)])
def api_intro_create(body: IntroCreate):
    return create_introduction(body.model_dump())


@app.put("/api/profile/introductions/{intro_id}", dependencies=[Depends(require_auth)])
def api_intro_update(intro_id: int, body: IntroUpdate):
    result = update_introduction(intro_id, body.model_dump(exclude_none=True))
    if not result:
        raise HTTPException(status_code=404, detail="Introduction not found")
    return result


@app.delete("/api/profile/introductions/{intro_id}", dependencies=[Depends(require_auth)])
def api_intro_delete(intro_id: int):
    if not delete_introduction(intro_id):
        raise HTTPException(status_code=404, detail="Introduction not found")
    return {"ok": True}


@app.patch("/api/profile/introductions/{intro_id}/main", dependencies=[Depends(require_auth)])
def api_intro_set_main(intro_id: int):
    result = set_main_introduction(intro_id)
    if not result:
        raise HTTPException(status_code=404, detail="Introduction not found")
    return result


# ── Todos API ──

@app.get("/api/todos")
def api_todos_list():
    return get_all_todos()


@app.post("/api/todos", status_code=201)
def api_todos_create(body: TodoCreate):
    if body.status not in ("todo", "in_progress", "done"):
        raise HTTPException(status_code=422, detail="status must be todo | in_progress | done")
    return create_todo(body.title, body.description, body.status)


# /done 라우트를 /{todo_id} 보다 먼저 등록해야 done이 id로 매칭되지 않음
@app.delete("/api/todos/done")
def api_todos_delete_done():
    deleted = delete_done_todos()
    return {"deleted": deleted}


@app.put("/api/todos/{todo_id}")
def api_todos_update(todo_id: str, body: TodoUpdate):
    if body.status is not None and body.status not in ("todo", "in_progress", "done"):
        raise HTTPException(status_code=422, detail="status must be todo | in_progress | done")
    updated = update_todo(todo_id, body.model_dump(exclude_none=True))
    if updated is None:
        raise HTTPException(status_code=404, detail="Todo not found")
    return updated


@app.delete("/api/todos/{todo_id}")
def api_todos_delete(todo_id: str):
    ok = delete_todo(todo_id)
    if not ok:
        raise HTTPException(status_code=404, detail="Todo not found")
    return {"ok": True}


# ── Blog API ──

@app.get("/api/blog/posts")
def api_blog_list():
    return {"posts": get_all_posts()}


@app.post("/api/blog/posts", status_code=201)
def api_blog_create(body: BlogPostCreate):
    from datetime import date as _date
    post_date = body.date if body.date else _date.today().isoformat()
    return create_post(body.title, body.body, body.excerpt, body.tags, post_date)


@app.put("/api/blog/posts/{post_id}")
def api_blog_update(post_id: int, body: BlogPostUpdate):
    updated = update_post(post_id, body.model_dump(exclude_none=True))
    if updated is None:
        raise HTTPException(status_code=404, detail="Post not found")
    return updated


@app.delete("/api/blog/posts/{post_id}")
def api_blog_delete(post_id: int):
    ok = delete_post(post_id)
    if not ok:
        raise HTTPException(status_code=404, detail="Post not found")
    return {"ok": True}
