"""
卖相AI - Flask 后端
按功能拆分为蓝图：认证(auth)、主页面(main)、管理员API(admin_api)、版本(version)
"""
import os
import secrets
from datetime import timedelta

from flask import Flask
from flask_cors import CORS

from utils.db import init_db
from blueprints.auth import auth
from blueprints.main import main
from blueprints.admin_api import admin_api
from blueprints.version import version_bp

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
app = Flask(__name__, template_folder=BASE_DIR, static_folder=BASE_DIR)

CORS(app)
app.secret_key = os.environ.get('SECRET_KEY', secrets.token_hex(32))
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(days=7)

# 注册蓝图
app.register_blueprint(auth)
app.register_blueprint(main)
app.register_blueprint(admin_api)
app.register_blueprint(version_bp)


def run_app(host='0.0.0.0', port=15124):
    init_db()
    app.run(host=host, port=port, threaded=True, use_reloader=False)


if __name__ == '__main__':
    run_app()
