from flask import Flask, render_template
from config import Config
from extensions import db, redis_client
from blueprints.auth import auth_bp
from blueprints.api import api_bp
from blueprints.admin import admin_bp
import threading
import time

# 导入模型（必须在 db.create_all() 之前导入）
import models

def create_app():
    app = Flask(__name__)
    app.config.from_object(Config)
    
    # 初始化扩展
    db.init_app(app)
    redis_client.init_app(app)
    
    # 注册蓝图
    app.register_blueprint(auth_bp)
    app.register_blueprint(api_bp)
    app.register_blueprint(admin_bp)
    
    @app.route('/')
    def index():
        return render_template('index.html')
        
    # 自动创建数据库表
    with app.app_context():
        print("🔧 正在检查并创建数据库表...")
        db.create_all()
        print("✅ 数据库表已就绪")
        
    return app

app = create_app()

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)
