import asyncio
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from loguru import logger

from app.config import settings
from app.database import init_db
from app.api import router


@asynccontextmanager
async def lifespan(app: FastAPI):
    logger.info("启动推拉酸洗线二级系统...")
    await init_db()
    await _create_default_admin()

    # 启动L1报文接收服务（UDP）
    from app.services.message_parser import l1_server
    import app.services.material_service  # noqa: 注册报文处理器
    await l1_server.start()

    logger.info("系统启动完成")
    yield

    l1_server.stop()
    logger.info("系统已停止")


async def _create_default_admin():
    """首次启动时创建默认管理员"""
    from app.database import AsyncSessionLocal
    from sqlalchemy import select
    from app.models.user import User
    from app.services.auth_service import hash_password

    async with AsyncSessionLocal() as db:
        result = await db.execute(select(User).where(User.username == "admin"))
        if not result.scalar_one_or_none():
            admin = User(
                username="admin",
                full_name="系统管理员",
                hashed_password=hash_password("admin123"),
                role="admin",
            )
            db.add(admin)
            await db.commit()
            logger.info("默认管理员已创建: admin / admin123")


app = FastAPI(
    title="推拉酸洗线二级系统",
    description="Pickling Line Level-2 MES System",
    version="1.0.0",
    lifespan=lifespan,
)

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

app.include_router(router, prefix="/api")


@app.get("/health")
async def health():
    return {"status": "ok", "service": "pickling-mes"}


if __name__ == "__main__":
    import uvicorn
    uvicorn.run("app.main:app", host=settings.APP_HOST, port=settings.APP_PORT, reload=settings.DEBUG)
