from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
from pathlib import Path
from app.routers import speakers, dialog # Import the routers
from app import config

app = FastAPI(
    title="Chatterbox TTS API",
    description="API for generating TTS dialogs using Chatterbox TTS.",
    version="0.1.0",
)

# CORS Middleware configuration
# For development, we'll allow all origins
# In production, you should restrict this to specific origins
app.add_middleware(
    CORSMiddleware,
    allow_origins=config.CORS_ORIGINS,
    allow_credentials=False,
    allow_methods=["*"],
    allow_headers=["*"],
    expose_headers=["*"]
)

# Include routers
app.include_router(speakers.router, prefix="/api/speakers", tags=["Speakers"])
app.include_router(dialog.router, prefix="/api/dialog", tags=["Dialog Generation"])

@app.get("/")
async def read_root():
    return {"message": "Welcome to the Chatterbox TTS API!"}

# Ensure the directory for serving generated audio exists
config.DIALOG_GENERATED_DIR.mkdir(parents=True, exist_ok=True)

# Mount StaticFiles to serve generated dialogs
app.mount("/generated_audio", StaticFiles(directory=config.DIALOG_GENERATED_DIR), name="generated_audio")

# Further endpoints for speakers, dialog generation, etc., will be added here.
