from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from app import __version__
from app.api.v1 import api_router, health
from app.core.config import settings
from app.middlewares.tenant import TenantHeaderMiddleware
from shared.exceptions import AppError
def create_app():
    app=FastAPI(title="Torbat Pay",version=__version__,description="Payment service phases 14.0-14.5")
    app.add_middleware(CORSMiddleware,allow_origins=settings.cors_origin_list,allow_credentials=False,allow_methods=["*"],allow_headers=["*"])
    app.add_middleware(TenantHeaderMiddleware)
    @app.exception_handler(AppError)
    async def handle(request:Request,exc:AppError): return JSONResponse(status_code=exc.status_code,content={"error":{"code":exc.error_code,"message":exc.message,"details":exc.details}})
    app.include_router(health.router)
    app.include_router(api_router,prefix=settings.api_v1_prefix)
    return app
app=create_app()
