import os
from flask import Blueprint, send_from_directory
# 👇 确保 config.py 在根目录，且能被引用
from config import get_static_path

web_bp = Blueprint('web', __name__)

@web_bp.route('/')
def index():
    """访问根路径时，返回 dist/index.html"""
    try:
        return send_from_directory(get_static_path(), 'index.html')
    except Exception as e:
        return f"前端资源未找到，请确认 dist 文件夹是否存在。错误信息: {e}", 404

@web_bp.route('/<path:path>')
def static_files(path):
    """访问 /css, /js 等静态资源"""
    static_folder = get_static_path()
    file_path = os.path.join(static_folder, path)

    if os.path.exists(file_path):
        return send_from_directory(static_folder, path)

    # 路由回退：解决 Vue History 模式刷新 404 问题
    # 如果找不到文件，就返回 index.html，让 Vue 路由去处理
    return send_from_directory(static_folder, 'index.html')