"""app_starter.py, capstone skeleton: deploy YOUR app behind a web API.

Plug your own logic into answer_question(): your M7/M8 RAG, your M9 agent, or any
LLM-powered idea. The FastAPI wrapper, health check, and logging are done for you.

Run locally:  uvicorn app_starter:app --reload   →   http://127.0.0.1:8000/docs
A worked example is in ../solution/app.py.
"""

import os
import time
import logging
from dotenv import load_dotenv
from fastapi import FastAPI
from pydantic import BaseModel
import anthropic

load_dotenv()
logging.basicConfig(level=logging.INFO, format="%(levelname)s %(message)s")
log = logging.getLogger("app")

client = anthropic.Anthropic()
MODEL = "claude-opus-4-8"

app = FastAPI(title="My Capstone")


class AskRequest(BaseModel):
    question: str


class AskResponse(BaseModel):
    answer: str


def answer_question(question: str) -> str:
    """TODO: replace this with YOUR capstone logic.
    Examples:
      - RAG (M7/M8): retrieve chunks from your vector store, then answer from them.
      - Agent (M9): run your tool-using agent on the question.
      - Anything LLM-powered that solves a real problem for you.
    For now it just asks the model directly, make it yours.
    """
    response = client.messages.create(
        model=MODEL, max_tokens=500,
        messages=[{"role": "user", "content": question}],
    )
    return response.content[0].text


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


@app.post("/ask", response_model=AskResponse)
def ask(request: AskRequest):
    start = time.time()
    answer = answer_question(request.question)
    log.info("POST /ask ok latency=%.2fs", time.time() - start)
    return AskResponse(answer=answer)
