from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from contextlib import asynccontextmanager

from app.common.database import engine, Base
from app.routers import notifications
from app.scheduler.scheduler import start_scheduler, stop_scheduler
from .routers import departments, advisors, schedules, bdc_router, ultravox_router, company

# Create the database tables
Base.metadata.create_all(bind=engine)

@asynccontextmanager
async def lifespan(app: FastAPI):
    # STARTUP
    start_scheduler()
    
    yield  # Application is running
    
    # SHUTDOWN
    stop_scheduler()

app = FastAPI(title="Advisor Service", lifespan=lifespan)

# Configure CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # In production, replace with specific origins
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Include routers
app.include_router(departments.router, prefix="/api/v1", tags=["departments"])
app.include_router(advisors.router, prefix="/api/v1", tags=["advisors"])
app.include_router(bdc_router.router, prefix="/api/v1", tags=["bdc"])
app.include_router(schedules.router, prefix="/api/v1", tags=["schedules"])
app.include_router(ultravox_router.router, prefix="/api/v1", tags=["ultravox"])
app.include_router(notifications.router, prefix="/api/v1", tags=["notifications"])
app.include_router(company.router, prefix="/api/v1", tags=["company"])

@app.get("/")
def read_root():
    return {"message": "Welcome to the Advisor Service"} 