from fastapi import FastAPI, Depends, Request, HTTPException
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse

from src.apps.auth.router import router as auth_router
from src.apps.users.router import router as user_router
from src.apps.datasource.router import router as datasource_router
from src.apps.stores.router import router
from src.mock_bright_data_api.router import router as mock_api
from src.mock_bright_data_api.router import bright_data_router as bright_data_webhook_router
from src.apps.feedback.router import router as webhook_router
from src.apps.feedback.router import feedback_router as feedback_router
from src.apps.sentiment.router import sentiment_router as sentiment_router
from src.apps.chatbot.chat_output import router as chat_router
from src.apps.recommendations.router import router as recommendation_router
from src.menu_design.apps.AI_chat.router import router as menu_design_router
from src.menu_design.apps.editor.router import router as editor_api_router
from src.menu_design.apps.projects.router import router as projects_router
from src.marketing.apps.Calendar.router import router as calendar_router
from src.marketing.apps.post.router import router as post_router
from src.marketing.apps.post.router import post_type_router,get_url_information
from src.marketing.apps.post.router import post_type_config,generate_content_router
from src.marketing.apps.Account.router import router as account_router,connected_router
from src.marketing.vector_db_collection.router import vector_db_router 
from src.marketing.apps.Account.router import fb_auth_router as fb_auth_router
from src.marketing.apps.Analytics.router import router as analytics_router
from src.marketing.apps.persona.router import router as persona_router
from src.marketing.apps.hwGpt.router import router as hw_gpt_router
from src.smart_inventory.router import main_router as smart_inventory_router
# Removed conflicting simple_websocket_router import
from src.utils.pusher_config import get_pusher_client

# Cron job imports
from src.marketing.apps.post.cron import start_cron
from src.marketing.apps.Analytics.cron import start_analytics_cron
# Smart Inventory Crons
from src.smart_inventory.jobs.products_fetch_cron import products_fetch_cron
from src.smart_inventory.jobs.daily_sales_cron import daily_sales_cron
from src.smart_inventory.jobs.service_level_cron import service_level_cron
from src.smart_inventory.jobs.inventory_snapshot_cron import inventory_snapshot_cron
from src.smart_inventory.jobs.slow_movers_cron import slow_movers_cron
from src.smart_inventory.jobs.inventory_planning_cron import inventory_planning_cron

import asyncio
import os


app = FastAPI(
    title="My API",
    description="API documentation with JWT Authentication",
    version="1.0",
    openapi_tags=[
        {"name": "Auth", "description": "Authentication endpoints"},
        {"name": "LLM PERSONA ANALYSIS", "description": "Azure LLM-powered store brand persona generation"},
        {"name": "HW GPT", "description": "Azure LLM-powered chatbot with thread management and chat history"}
    ]
)



# #custome exception handler


@app.exception_handler(HTTPException)
async def custom_http_exception_handler(request: Request, exc: HTTPException):
    detail = exc.detail if isinstance(exc.detail, dict) else {
        "success": False,
        "message": str(exc.detail),
        "data": []
    }
    return JSONResponse(
        status_code=exc.status_code,
        content=detail
    )





# Enable CORS 
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*","http://localhost:3000","http://localhost:5173","https://sentiment-hb-ui.dreamztesting.com","https://hubwalletmenu-dev.dreamztesting.com"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Serve static files (JSON and public folder)
app.mount(
    "/static", 
    StaticFiles(directory="./src/menu_design/designer/static"), 
    name="static"
)

#For cron purpose
@app.on_event("startup")
def startup_event():
    start_cron()
    start_analytics_cron()

    # smart inventory cron jobs
    # products_fetch_cron() # disabled for now as instructed, working with dummy data
    daily_sales_cron()
    inventory_snapshot_cron()
    service_level_cron()
    slow_movers_cron()
    inventory_planning_cron()

# Include routers
app.include_router(auth_router, prefix="/auth", tags=["AUTH"])
app.include_router(user_router, prefix="/users", tags=["AUTH"])

app.include_router(router, prefix="/stores", tags=["STORE MANAGEMENT"])
app.include_router(datasource_router, prefix="/datasource",tags=["DATASOURCE ADD"])
app.include_router(mock_api, prefix="/mock_router", tags=["Internal API"])
app.include_router(webhook_router, prefix="/webhook", tags=["Internal API"])
app.include_router(feedback_router, prefix="/feedback", tags=["SENTIMENT ANALYSIS"])
app.include_router(sentiment_router, prefix="/feedback", tags=["DETAILED ANALYSIS OF REVIEWS, RATINGS, WORDS AND TOPICS"])
app.include_router(chat_router, prefix="/chat", tags=["CHATBOT OUTPUT"])
app.include_router(recommendation_router, prefix="/recommendation", tags=["RECOMMENDATION OUTPUT"])
app.include_router(bright_data_webhook_router, tags=["Internal API"])
app.include_router(menu_design_router, tags=["Menu Design"])

# Projects router
app.include_router(projects_router, prefix="/projects", tags=["PROJECTS"])
app.include_router(editor_api_router, tags=["Editor_API"])

#Calendar router
app.include_router(calendar_router, tags=["CALENDAR MANAGEMENT"])

# Post router
app.include_router(post_router, tags=["POST MANAGEMENT"])
app.include_router(post_type_router, tags=["POST TYPE MANAGEMENT"])
app.include_router(post_type_config, tags=["POST TYPE CONFIGURATION MANAGEMENT"])
app.include_router(generate_content_router, tags=["GENERATE CONTENT MANAGEMENT"])

#Account router
app.include_router(account_router)
app.include_router(connected_router)
app.include_router(fb_auth_router)

#Vector Data Base Router
app.include_router(vector_db_router,tags=["VECTOR DB COLLECTION"])

app.include_router(get_url_information, tags=["GET URL INFORMATION"])
app.include_router(analytics_router, tags=["TWITTER ANALYTICS"])
app.include_router(persona_router, prefix="/persona", tags=["LLM PERSONA ANALYSIS"])
app.include_router(hw_gpt_router, tags=["HW GPT"])
app.include_router(smart_inventory_router, prefix="/smart-inventory")
# Removed conflicting simple_websocket_router include

