import asyncio
import sys
from contextlib import asynccontextmanager

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

from app.core.config import settings
from app.core.mode_middleware import LandingModeMiddleware
from app.routers.contact_router import router as contact_router

# В режиме landing не загружаем роутеры, требующие БД
if settings.app_mode != "landing":
    from app.core.session_middleware import SessionMiddleware
    from app.routers import resume_router, vacancy_router
    from app.routers.admin_router import router as admin_router
    from app.routers.analysis_router import router as analysis_router
    from app.routers.interview_reports_router import router as interview_report_router
    from app.routers.interview_router import router as interview_router
    from app.routers.session_router import router as session_router


@asynccontextmanager
async def lifespan(app: FastAPI):
    # В режиме landing агент не нужен
    if settings.app_mode == "landing":
        print("[STARTUP] Landing mode - skipping AI Agent startup")
        yield
        print("[SHUTDOWN] Landing mode - no AI Agent to stop")
        return

    # Запускаем AI агента при старте приложения
    from app.services.agent_manager import agent_manager

    if sys.platform.startswith("win"):
        asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())

    print("[STARTUP] Starting AI Agent...")
    success = await agent_manager.start_agent()

    if success:
        print("[STARTUP] AI Agent started successfully")
    else:
        print("[STARTUP] Failed to start AI Agent")

    yield

    # Останавливаем AI агента при завершении приложения
    print("[SHUTDOWN] Stopping AI Agent...")
    await agent_manager.stop_agent()
    print("[SHUTDOWN] AI Agent stopped")


app = FastAPI(
    title="HR AI Backend",
    description="Backend API for HR AI system with vacancies and resumes management",
    version="1.0.0",
    lifespan=lifespan,
)

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

# Добавляем middleware для режима лендинга (блокирует API в режиме landing)
app.add_middleware(LandingModeMiddleware)

# Contact router доступен всегда (не требует БД)
app.include_router(contact_router, prefix="/api/v1")

# Остальные роутеры и session middleware только в app режиме
if settings.app_mode != "landing":
    app.add_middleware(SessionMiddleware, cookie_name="session_id")
    app.include_router(vacancy_router, prefix="/api/v1")
    app.include_router(resume_router, prefix="/api/v1")
    app.include_router(session_router, prefix="/api/v1")
    app.include_router(interview_router, prefix="/api/v1")
    app.include_router(analysis_router, prefix="/api/v1")
    app.include_router(admin_router, prefix="/api/v1")
    app.include_router(interview_report_router, prefix="/api/v1")


@app.get("/")
async def root():
    return {
        "message": "HR AI Backend API",
        "version": "1.0.0",
        "mode": settings.app_mode,
    }


@app.get("/health")
async def health_check():
    return {"status": "healthy"}


@app.get("/api/v1/config/mode")
async def get_app_mode():
    """
    Get current application mode.
    Returns 'landing' for landing page mode or 'app' for main application.
    """
    return {
        "mode": settings.app_mode,
        "description": "landing" if settings.app_mode == "landing"
        else "Main application mode",
    }
