"""
FastAPI application entrypoint
"""
import logging
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from app.core.config import settings
from app.routers.config import router as config_router
from app.routers.financial import router as financial_router
from app.routers.orgs import router as orgs_router

# Configure logging to ensure our app logs show up in development
import sys

# Force our logging configuration to override uvicorn's
class ForcefulHandler(logging.Handler):
    def emit(self, record):
        # Force output to stdout regardless of uvicorn's configuration
        print(f"[APP] {record.getMessage()}", file=sys.stdout, flush=True)

# Set up our forceful handler for data providers
forceful_handler = ForcefulHandler()
forceful_handler.setLevel(logging.DEBUG)

# Configure data providers logger with forceful output
data_providers_logger = logging.getLogger('app.data_providers')
data_providers_logger.setLevel(logging.DEBUG)
data_providers_logger.addHandler(forceful_handler)

# Also set up for the main app logger
app_logger = logging.getLogger('app')
app_logger.setLevel(logging.INFO)
app_logger.addHandler(forceful_handler)

# Ensure our handlers are not suppressed
data_providers_logger.propagate = False
app_logger.propagate = False

app = FastAPI(title=settings.APP_NAME, version=settings.APP_VERSION)

# CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Routers
app.include_router(config_router, prefix=f"{settings.API_V1_STR}/config", tags=["config"])
app.include_router(financial_router, prefix=f"{settings.API_V1_STR}/financials", tags=["financials"])
app.include_router(orgs_router, prefix=f"{settings.API_V1_STR}/orgs", tags=["orgs"])

@app.get("/")
async def root():
    return {"status": "ok", "name": settings.APP_NAME, "version": settings.APP_VERSION}
