from flask import Flask, jsonify, send_from_directory, request
from flask_cors import CORS
import json
import os
import random

# 检测运行模式:通过环境变量控制
RUN_MODE = os.environ.get('RUN_MODE', 'development')  # development 或 production

# 根据运行模式配置
if RUN_MODE == 'production':
    # 生产环境:使用构建后的前端
    FRONTEND_BUILD_PATH = os.path.join(os.path.dirname(__file__), '..', 'frontend', 'build')
    app = Flask(__name__, static_folder=FRONTEND_BUILD_PATH, static_url_path='')
    BACKGROUND_DIR = os.path.join(FRONTEND_BUILD_PATH, 'background')
else:
    # 开发环境:不服务前端,只提供 API
    app = Flask(__name__)
    BACKGROUND_DIR = os.path.join(os.path.dirname(__file__), '..', 'mengyaprofile-frontend', 'public', 'background')

CORS(app)  # 允许跨域请求

# 数据文件路径 - 支持环境变量配置
DATA_DIR = os.environ.get('DATA_DIR', os.path.join(os.path.dirname(__file__), 'data'))

def load_json_file(filename):
    """加载JSON文件"""
    try:
        with open(os.path.join(DATA_DIR, filename), 'r', encoding='utf-8') as f:
            return json.load(f)
    except FileNotFoundError:
        return None
    except Exception as e:
        print(f"Error loading {filename}: {e}")
        return None

@app.route('/api/profile', methods=['GET'])
def get_profile():
    """获取个人基本信息"""
    data = load_json_file('profile.json')
    if data:
        return jsonify(data)
    return jsonify({"error": "Profile没有找到"}), 404

@app.route('/api/projects', methods=['GET'])
def get_projects():
    """获取精选项目列表"""
    data = load_json_file('projects.json')
    if data:
        return jsonify(data)
    return jsonify({"error": "Projects没有找到"}), 404

@app.route('/api/contacts', methods=['GET'])
def get_contacts():
    """获取联系方式"""
    data = load_json_file('contacts.json')
    if data:
        return jsonify(data)
    return jsonify({"error": "Contacts没有找到"}), 404

@app.route('/api/techstack', methods=['GET'])
def get_techstack():
    """获取技术栈"""
    data = load_json_file('techstack.json')
    if data:
        return jsonify(data)
    return jsonify({"error": "Tech stack没有找到"}), 404

@app.route('/api/random-background', methods=['GET'])
def get_random_background():
    """获取随机背景图片"""
    try:
        # 获取背景图片目录中的所有图片
        if os.path.exists(BACKGROUND_DIR):
            images = [f for f in os.listdir(BACKGROUND_DIR) 
                     if f.lower().endswith(('.png', '.jpg', '.jpeg', '.webp', '.gif'))]
            if images:
                random_image = random.choice(images)
                return jsonify({"image": f"/background/{random_image}"})
        return jsonify({"image": None})
    except Exception as e:
        print(f"获取随机背景出错: {e}")
        return jsonify({"image": None})

@app.route('/api/all', methods=['GET'])
def get_all():
    """获取所有数据"""
    profile = load_json_file('profile.json')
    projects = load_json_file('projects.json')
    contacts = load_json_file('contacts.json')
    techstack = load_json_file('techstack.json')
    
    return jsonify({
        "profile": profile,
        "techstack": techstack,
        "projects": projects,
        "contacts": contacts
    })

@app.route('/', methods=['GET'])
def index():
    """服务前端页面或API信息"""
    if RUN_MODE == 'production' and app.static_folder:
        # 生产环境,返回前端页面
        return send_from_directory(app.static_folder, 'index.html')
    else:
        # 开发环境,返回API信息
        return jsonify({
            "message": "萌芽主页 后端API - 开发模式",
            "author": "树萌芽",
            "version": "1.0.0",
            "mode": "development",
            "note": "前端开发服务器运行在 http://localhost:3000",
            "endpoints": {
                "/api/profile": "获取个人信息",
                "/api/techstack": "获取技术栈",
                "/api/projects": "获取项目列表",
                "/api/contacts": "获取联系方式",
                "/api/random-background": "获取随机背景图片",
                "/api/all": "获取所有数据"
            }
        })

@app.route('/admin')
def admin():
    """服务管理员页面(也是前端)"""
    if RUN_MODE == 'production' and app.static_folder:
        return send_from_directory(app.static_folder, 'index.html')
    else:
        return jsonify({
            "error": "开发模式",
            "note": "请访问 http://localhost:3000/admin?token=shumengya520"
        }), 404

@app.route('/api')
def api_info():
    """API信息"""
    return jsonify({
        "message": "萌芽主页 后端API",
        "author":"树萌芽",
        "version": "1.0.0",
        "endpoints": {
            "/api/profile": "获取个人信息",
            "/api/techstack": "获取技术栈",
            "/api/projects": "获取项目列表",
            "/api/contacts": "获取联系方式",
            "/api/random-background": "获取随机背景图片",
            "/api/all": "获取所有数据"
        }
    })

# 处理前端路由 - 所有非API请求都返回 index.html
@app.errorhandler(404)
def not_found(e):
    """处理404错误"""
    # 检查是否为API请求
    if request.path.startswith('/api'):
        return jsonify({"error": "API endpoint not found"}), 404
    
    # 非API请求
    if RUN_MODE == 'production' and app.static_folder:
        # 生产环境返回前端页面(支持前端路由)
        return send_from_directory(app.static_folder, 'index.html')
    
    # 开发环境
    return jsonify({
        "error": "页面未找到",
        "mode": "development",
        "note": "开发环境请访问 http://localhost:3000"
    }), 404

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