#!/usr/bin/env python3
"""
开发环境启动脚本
使用SQLite数据库进行本地开发测试
"""
import os
import sys
import asyncio
from pathlib import Path

# 设置环境变量
os.environ["DATABASE_URL"] = "sqlite+aiosqlite:///./test.db"
os.environ["SECRET_KEY"] = "dev-secret-key-for-testing-only-not-for-production"
os.environ["DEBUG"] = "true"
os.environ["LOG_LEVEL"] = "INFO"
os.environ["LOG_FORMAT"] = "console"

# 添加项目根目录到Python路径
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))

async def create_tables():
    """创建数据库表"""
    try:
        from app.config.database import engine
        from app.models.base import Base
        
        async with engine.begin() as conn:
            await conn.run_sync(Base.metadata.create_all)
        print("✅ 数据库表创建成功")
    except Exception as e:
        print(f"❌ 数据库表创建失败: {e}")

async def main():
    """主函数"""
    print("🚀 启动考培练系统后端服务...")
    
    # 创建数据库表
    await create_tables()
    
    # 启动服务
    import uvicorn
    from app.main import app
    
    print("📚 API文档地址: http://localhost:8000/api/v1/docs")
    print("🔍 健康检查: http://localhost:8000/health")
    print("⏹️  按 Ctrl+C 停止服务")
    
    uvicorn.run(
        app,
        host="0.0.0.0",
        port=8000,
        reload=True,
        log_level="info"
    )

if __name__ == "__main__":
    asyncio.run(main())
