from flask import Flask, render_template, request, jsonify, send_file, Response, session
import os
import time
import json
from datetime import datetime
from threading import Lock
from HOS_ME.utils.report_generator import Config, ReportGenerator
from HOS_ME.utils.api_client import APIClient
from HOS_ME.utils.template_manager import TemplateManager
from HOS_ME.utils.meeting_manager import MeetingManager
from HOS_ME.utils.project_manager import ProjectManager
from HOS_ME.utils.knowledge_base import KnowledgeBase
from HOS_ME.utils.schedule_manager import ScheduleManager
from HOS_ME.utils.approval_manager import ApprovalManager
from HOS_ME.utils.task_manager import TaskManager
from HOS_ME.utils.document_converter import DocumentConverter
from HOS_ME.utils.kroki_renderer import KrokiRenderer
from HOS_ME.utils.ascii_image_converter import ASCIIImageConverter
from HOS_ME.utils.batch_processor import BatchProcessor
from HOS_ME.utils.i18n import i18n

# 获取当前文件所在目录
current_dir = os.path.dirname(os.path.abspath(__file__))

# 初始化Flask应用，配置模板和静态文件夹路径
app = Flask(__name__, 
            template_folder=os.path.join(current_dir, 'templates'), 
            static_folder=os.path.join(current_dir, 'static'))

# 设置secret key用于session管理
app.config['SECRET_KEY'] = os.urandom(24)

# 设置支持的语言
app.config['SUPPORTED_LOCALES'] = ['zh', 'en']

# 语言选择函数
def get_locale():
    # 从session获取语言设置，默认中文
    return session.get('language', 'zh')

# 语言切换路由
@app.route('/api/set_language', methods=['POST'])
def set_language():
    data = request.get_json()
    language = data.get('language', 'zh')
    if language in app.config['SUPPORTED_LOCALES']:
        session['language'] = language
        i18n.set_locale(language)
        return jsonify({'success': True, 'message': i18n.translate('语言设置成功', language)})
    return jsonify({'success': False, 'message': i18n.translate('不支持的语言')})

# 添加模板上下文处理器，让模板可以使用翻译函数
@app.context_processor
def inject_i18n():
    def _(text):
        return i18n.translate(text, get_locale())
    return {'_': _}


# 全局变量
config = Config()
api_client = APIClient(config.api_key)
report_generator = ReportGenerator(config, api_client)
template_manager = TemplateManager()
meeting_manager = MeetingManager()
project_manager = ProjectManager()
knowledge_base = KnowledgeBase(config)  # 传递config参数
schedule_manager = ScheduleManager()
approval_manager = ApprovalManager()

# 新增文档转换相关工具
document_converter = DocumentConverter()
kroki_renderer = KrokiRenderer()
ascii_converter = ASCIIImageConverter()
batch_processor = BatchProcessor()

# 任务管理器
app_task_manager = TaskManager()

# 进度管理：用于存储和推送生成进度
progress_data = {}
progress_lock = Lock()
progress_counter = 0

# 主页面路由
@app.route('/')
def index():
    return render_template('index.html')

# 单周报生成路由
@app.route('/api/generate', methods=['POST'])
def generate_report():
    try:
        data = request.get_json()
        prompt = data.get('prompt', '')
        template_id = data.get('template_id', None)
        rag_library_id = data.get('rag_library_id', None)
        
        if not prompt.strip():
            return jsonify({'success': False, 'message': '请输入提示词'})
        
        # 生成进度ID
        global progress_counter
        with progress_lock:
            progress_counter += 1
            progress_id = f'progress_{progress_counter}'
        
        # 进度回调函数
        def progress_callback(current, total, percentage, message):
            update_progress(progress_id, current, total, percentage, message)
        
        # 添加进度支持
        update_progress(progress_id, 0, 1, 0, '准备生成周报...')
        
        report_content = report_generator.generate_single_report(prompt, template_id, progress_callback, rag_library_id)
        
        update_progress(progress_id, 1, 1, 100, '生成完成')
        
        return jsonify({'success': True, 'content': report_content, 'progress_id': progress_id})
    except Exception as e:
        return jsonify({'success': False, 'message': f'生成失败: {str(e)}'})

def update_progress(progress_id, current, total, percentage, message):
    """更新生成进度"""
    with progress_lock:
        progress_data[progress_id] = {
            'current': current,
            'total': total,
            'percentage': round(percentage, 2),
            'message': message,
            'timestamp': time.time()
        }

# SSE端点：推送生成进度
@app.route('/api/generate/progress/<progress_id>')
def generate_progress(progress_id):
    """推送生成进度的SSE端点"""
    def generate():
        with progress_lock:
            # 初始化进度
            if progress_id not in progress_data:
                progress_data[progress_id] = {
                    'current': 0,
                    'total': 0,
                    'percentage': 0,
                    'message': '准备开始生成...',
                    'timestamp': time.time()
                }
        
        while True:
            with progress_lock:
                progress = progress_data.get(progress_id)
                if not progress:
                    break
            
            # 发送进度更新
            yield f'data: {json.dumps(progress)}\n\n'
            
            # 检查是否完成
            if progress['percentage'] >= 100:
                break
            
            # 每0.5秒更新一次
            time.sleep(0.5)
        
        # 清理进度数据
        with progress_lock:
            if progress_id in progress_data:
                del progress_data[progress_id]
    
    return Response(generate(), mimetype='text/event-stream')

# 批量提示词生成路由
@app.route('/api/generate_batch_prompts', methods=['POST'])
def generate_batch_prompts():
    try:
        data = request.get_json()
        document_type = data.get('document_type', 'weekly_report')
        prompt_count = data.get('prompt_count', 5)
        
        # 根据文档类型生成不同的提示词生成提示
        if document_type == 'weekly_report':
            generate_prompt = f"请生成{prompt_count}个不同的周报提示词，每个提示词占一行，内容要多样化，涵盖不同行业和工作内容。每个提示词应该具体、可执行，能够生成完整的周报。"
        elif document_type == 'meeting_minutes':
            generate_prompt = f"请生成{prompt_count}个不同的会议纪要提示词，每个提示词占一行，内容要多样化，涵盖不同类型的会议。每个提示词应该具体、可执行，能够生成完整的会议纪要。"
        elif document_type == 'project_plan':
            generate_prompt = f"请生成{prompt_count}个不同的项目计划提示词，每个提示词占一行，内容要多样化，涵盖不同类型的项目。每个提示词应该具体、可执行，能够生成完整的项目计划。"
        else:
            generate_prompt = f"请生成{prompt_count}个不同的文档提示词，每个提示词占一行，内容要多样化，涵盖不同主题。每个提示词应该具体、可执行，能够生成完整的文档。"
        
        # 调用AI API生成提示词
        generated_text = api_client.generate_report(generate_prompt)
        
        # 处理生成结果，提取每行作为一个提示词
        prompts = generated_text.strip().split('\n')
        
        # 过滤掉空行和太短的提示词
        filtered_prompts = [p.strip() for p in prompts if p.strip() and len(p.strip()) > 10]
        
        # 如果生成的提示词数量不足，添加一些默认提示词
        if len(filtered_prompts) < prompt_count:
            default_prompts = [
                "写一份关于项目进度的周报，包含本周完成的工作、遇到的问题和下周计划",
                "生成一份团队周会的会议纪要，记录讨论的主要内容和决议",
                "创建一份新产品开发项目的计划，包含时间线和资源分配",
                "撰写一份市场调研报告，分析行业趋势和竞争对手",
                "生成一份季度工作总结，总结成果和经验教训"
            ]
            
            for default_prompt in default_prompts:
                if len(filtered_prompts) < prompt_count and default_prompt not in filtered_prompts:
                    filtered_prompts.append(default_prompt)
        
        # 只返回请求数量的提示词
        filtered_prompts = filtered_prompts[:prompt_count]
        
        return jsonify({
            'success': True,
            'prompts': filtered_prompts
        })
    except Exception as e:
        print(f"生成批量提示词失败: {str(e)}")
        return jsonify({
            'success': False,
            'message': f"生成提示词失败: {str(e)}"
        })

# 批量周报生成路由
@app.route('/api/batch_generate', methods=['POST'])
def batch_generate_reports():
    try:
        data = request.get_json()
        prompts = data.get('prompts', '')
        template_id = data.get('template_id', None)
        file_format = data.get('file_format', 'txt')
        rag_library_id = data.get('rag_library_id', None)
        
        if not prompts.strip():
            return jsonify({'success': False, 'message': '请输入提示词'})
        
        prompts_list = [p.strip() for p in prompts.split('\n') if p.strip()]
        
        # 生成进度ID
        global progress_counter
        with progress_lock:
            progress_counter += 1
            progress_id = f'progress_{progress_counter}'
        
        # 创建任务
        task_id = app_task_manager.create_task(
            task_type="batch_generate",
            description=f"批量生成 {len(prompts_list)} 份文档",
            total_steps=len(prompts_list)
        )
        
        # 进度回调函数
        def progress_callback(current, total, percentage, message):
            update_progress(progress_id, current, total, percentage, message)
            app_task_manager.update_task(
                task_id,
                current_step=current,
                percentage=percentage
            )
            # 检查任务是否被暂停
            app_task_manager.wait_for_resume(task_id)
        
        # 异步执行批量生成
        import threading
        results = []
        
        def batch_generate_task():
            nonlocal results
            try:
                results = report_generator.generate_batch_reports(prompts_list, template_id, progress_callback, file_format, rag_library_id)
                app_task_manager.complete_task(task_id, result={"generated": len(results), "results": results})
            except Exception as e:
                app_task_manager.fail_task(task_id, error=str(e))
        
        thread = threading.Thread(target=batch_generate_task)
        thread.daemon = True
        thread.start()
        
        # 返回进度ID和任务ID，客户端可以通过这些ID获取实时进度和任务状态
        return jsonify({
            'success': True, 
            'progress_id': progress_id,
            'task_id': task_id,
            'message': '批量生成已开始，请通过进度ID获取实时进度，通过任务ID管理任务'
        })
    except Exception as e:
        return jsonify({'success': False, 'message': f'批量生成失败: {str(e)}'})

# 保存周报路由
@app.route('/api/save', methods=['POST'])
def save_report():
    try:
        data = request.get_json()
        content = data.get('content', '')
        filename = data.get('filename', '')
        file_format = data.get('file_format', 'txt')
        
        if not content.strip():
            return jsonify({'success': False, 'message': '请输入周报内容'})
        
        saved_filename = report_generator.save_report(content, filename, file_format)
        return jsonify({'success': True, 'filename': saved_filename})
    except Exception as e:
        return jsonify({'success': False, 'message': f'保存失败: {str(e)}'})

# 加载周报列表路由
@app.route('/api/load_reports', methods=['GET'])
def load_reports():
    try:
        reports = report_generator.load_reports()
        # 只返回文件名和日期
        report_list = [{'filename': r['filename'], 'date': r['date']} for r in reports]
        return jsonify({'success': True, 'reports': report_list})
    except Exception as e:
        return jsonify({'success': False, 'message': f'加载失败: {str(e)}'})

# 加载特定周报路由
@app.route('/api/load_report/<filename>', methods=['GET'])
def load_report(filename):
    try:
        content = report_generator.read_report(filename)
        return jsonify({'success': True, 'content': content})
    except Exception as e:
        return jsonify({'success': False, 'message': f'加载失败: {str(e)}'})

# 删除周报路由
@app.route('/api/delete_report/<filename>', methods=['DELETE'])
def delete_report(filename):
    try:
        success = report_generator.delete_report(filename)
        if success:
            return jsonify({'success': True, 'message': '删除成功'})
        else:
            return jsonify({'success': False, 'message': '删除失败，文件不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'删除失败: {str(e)}'})

# 批量删除周报路由
@app.route('/api/batch_delete_reports', methods=['POST'])
def batch_delete_reports():
    try:
        data = request.get_json()
        filenames = data.get('filenames', [])
        
        if not filenames:
            return jsonify({'success': False, 'message': '请选择要删除的文件'})
        
        # 生成进度ID
        global progress_counter
        with progress_lock:
            progress_counter += 1
            progress_id = f'progress_{progress_counter}'
        
        # 进度回调函数
        def progress_callback(current, total, percentage, message):
            update_progress(progress_id, current, total, percentage, message)
        
        # 异步执行批量删除
        import threading
        result = {}
        
        def batch_delete_task():
            nonlocal result
            result = report_generator.batch_delete_reports(filenames, progress_callback)
        
        thread = threading.Thread(target=batch_delete_task)
        thread.daemon = True
        thread.start()
        
        # 返回进度ID，客户端可以通过这个ID获取实时进度
        return jsonify({
            'success': True, 
            'progress_id': progress_id,
            'message': '批量删除已开始，请通过进度ID获取实时进度'
        })
    except Exception as e:
        return jsonify({'success': False, 'message': f'批量删除失败: {str(e)}'})

# Excel导入路由
@app.route('/api/import_excel', methods=['POST'])
def import_excel():
    try:
        # 检查是否有文件上传
        if 'file' not in request.files:
            return jsonify({'success': False, 'message': '请选择要上传的Excel文件'})
        
        file = request.files['file']
        
        # 检查文件是否为空
        if file.filename == '':
            return jsonify({'success': False, 'message': '请选择要上传的Excel文件'})
        
        # 检查文件类型
        if not file.filename.endswith(('.xlsx', '.xls')):
            return jsonify({'success': False, 'message': '请选择Excel文件（.xlsx或.xls格式）'})
        
        # 生成进度ID
        global progress_counter
        with progress_lock:
            progress_counter += 1
            progress_id = f'progress_{progress_counter}'
        
        # 进度回调函数
        def progress_callback(current, total, percentage, message):
            update_progress(progress_id, current, total, percentage, message)
        
        # 异步执行Excel导入
        import threading
        result = {}
        
        def import_excel_task():
            nonlocal result
            result = report_generator.import_excel(file, progress_callback)
        
        thread = threading.Thread(target=import_excel_task)
        thread.daemon = True
        thread.start()
        
        # 返回进度ID，客户端可以通过这个ID获取实时进度
        return jsonify({
            'success': True, 
            'progress_id': progress_id,
            'message': 'Excel导入已开始，请通过进度ID获取实时进度'
        })
    except Exception as e:
        return jsonify({'success': False, 'message': f'Excel导入失败: {str(e)}'})

# 切换API来源路由
@app.route('/api/switch_api', methods=['POST'])
def switch_api():
    try:
        data = request.get_json()
        api_source = data.get('api_source', 'deepseek')
        
        success = api_client.set_api_source(api_source)
        if success:
            # 更新report_generator的api_client
            report_generator.api_client = api_client
            status_msg = "DeepSeek API已连接" if api_source == "deepseek" else "本地Ollama已连接"
            return jsonify({'success': True, 'status': status_msg})
        else:
            return jsonify({'success': False, 'message': '切换API失败，未知来源'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'切换API失败: {str(e)}'})

# 下载周报路由
@app.route('/api/download/<filename>', methods=['GET'])
def download_report(filename):
    try:
        filepath = os.path.join(config.reports_dir, filename)
        if os.path.exists(filepath):
            return send_file(filepath, as_attachment=True)
        else:
            return jsonify({'success': False, 'message': '文件不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'下载失败: {str(e)}'})

# 模板管理API

# 获取所有模板
@app.route('/api/templates', methods=['GET'])
def get_templates():
    try:
        templates = template_manager.get_templates()
        return jsonify({'success': True, 'templates': templates})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取模板列表失败: {str(e)}'})

# 获取当前模板
@app.route('/api/templates/current', methods=['GET'])
def get_current_template():
    try:
        template = template_manager.get_current_template()
        return jsonify({'success': True, 'template': template})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取当前模板失败: {str(e)}'})

# 获取指定模板
@app.route('/api/templates/<template_id>', methods=['GET'])
def get_template(template_id):
    try:
        template = template_manager.get_template(template_id)
        if template:
            return jsonify({'success': True, 'template': template})
        else:
            return jsonify({'success': False, 'message': '模板不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取模板失败: {str(e)}'})

# 创建模板
@app.route('/api/templates', methods=['POST'])
def create_template():
    try:
        data = request.get_json()
        name = data.get('name', '')
        content = data.get('content', '')
        description = data.get('description', '')
        
        if not name or not content:
            return jsonify({'success': False, 'message': '模板名称和内容不能为空'})
        
        template = template_manager.create_template(name, content, description)
        return jsonify({'success': True, 'template': template})
    except Exception as e:
        return jsonify({'success': False, 'message': f'创建模板失败: {str(e)}'})

# 更新模板
@app.route('/api/templates/<template_id>', methods=['PUT'])
def update_template(template_id):
    try:
        data = request.get_json()
        name = data.get('name')
        content = data.get('content')
        description = data.get('description')
        
        template = template_manager.update_template(template_id, name, content, description)
        if template:
            return jsonify({'success': True, 'template': template})
        else:
            return jsonify({'success': False, 'message': '模板不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'更新模板失败: {str(e)}'})

# 删除模板
@app.route('/api/templates/<template_id>', methods=['DELETE'])
def delete_template(template_id):
    try:
        success = template_manager.delete_template(template_id)
        if success:
            return jsonify({'success': True, 'message': '模板删除成功'})
        else:
            return jsonify({'success': False, 'message': '模板不存在或无法删除'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'删除模板失败: {str(e)}'})

# 设置当前模板
@app.route('/api/templates/current', methods=['PUT'])
def set_current_template():
    try:
        data = request.get_json()
        template_id = data.get('template_id', '')
        
        success = template_manager.set_current_template(template_id)
        if success:
            return jsonify({'success': True, 'message': '当前模板设置成功'})
        else:
            return jsonify({'success': False, 'message': '模板不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'设置当前模板失败: {str(e)}'})

# 导入模板
@app.route('/api/templates/import', methods=['POST'])
def import_template():
    try:
        data = request.get_json()
        name = data.get('name', '')
        content = data.get('content', '')
        description = data.get('description', '')
        
        if not name or not content:
            return jsonify({'success': False, 'message': '模板名称和内容不能为空'})
        
        template = template_manager.import_template(name, content, description)
        return jsonify({'success': True, 'template': template})
    except Exception as e:
        return jsonify({'success': False, 'message': f'导入模板失败: {str(e)}'})

# 导入DOCX模板
@app.route('/api/templates/import_docx', methods=['POST'])
def import_docx_template():
    try:
        # 检查是否有文件上传
        if 'file' not in request.files:
            return jsonify({'success': False, 'message': '请选择要上传的DOCX文件'})
        
        file = request.files['file']
        
        # 检查文件类型
        if not file.filename.endswith('.docx'):
            return jsonify({'success': False, 'message': '请上传DOCX格式的文件'})
        
        # 获取其他参数
        name = request.form.get('name', '').strip()
        description = request.form.get('description', '').strip()
        template_type = request.form.get('type', 'weekly_report')
        
        # 如果没有提供名称，使用文件名作为模板名称
        if not name:
            name = os.path.splitext(file.filename)[0]
        
        # 保存上传的文件到临时位置
        temp_file_path = os.path.join(tempfile.gettempdir(), file.filename)
        file.save(temp_file_path)
        
        # 导入DOCX模板
        template = template_manager.import_docx_template(
            temp_file_path, 
            name, 
            description, 
            template_type
        )
        
        # 删除临时文件
        os.remove(temp_file_path)
        
        if template:
            return jsonify({'success': True, 'template': template, 'message': 'DOCX模板导入成功'})
        else:
            return jsonify({'success': False, 'message': '导入DOCX模板失败'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'导入DOCX模板失败: {str(e)}'})

# 导出模板
@app.route('/api/templates/<template_id>/export', methods=['GET'])
def export_template(template_id):
    try:
        template = template_manager.export_template(template_id)
        if template:
            return jsonify({'success': True, 'template': template})
        else:
            return jsonify({'success': False, 'message': '模板不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'导出模板失败: {str(e)}'})

# 派生模板
@app.route('/api/templates/<template_id>/derive', methods=['POST'])
def derive_template(template_id):
    try:
        data = request.get_json()
        new_name = data.get('name', '')
        new_description = data.get('description', '')
        
        if not new_name:
            return jsonify({'success': False, 'message': '新模板名称不能为空'})
        
        new_template = template_manager.derive_template(template_id, new_name, new_description)
        if new_template:
            return jsonify({'success': True, 'template': new_template})
        else:
            return jsonify({'success': False, 'message': '源模板不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'派生模板失败: {str(e)}'})

# 获取模板结构
@app.route('/api/templates/<template_id>/structure', methods=['GET'])
def get_template_structure(template_id):
    try:
        structure = template_manager.get_template_structure(template_id)
        return jsonify({'success': True, 'structure': structure})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取模板结构失败: {str(e)}'})

# 系统设置API

# 获取模板配置
@app.route('/api/system/template-settings', methods=['GET'])
def get_template_settings():
    try:
        settings = config.get_template_settings()
        return jsonify({'success': True, 'settings': settings})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取模板配置失败: {str(e)}'})

# 更新模板配置
@app.route('/api/system/template-settings', methods=['PUT'])
def update_template_settings():
    try:
        data = request.get_json()
        config.update_template_settings(data)
        return jsonify({'success': True, 'message': '模板配置更新成功'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'更新模板配置失败: {str(e)}'})

# 获取系统配置
@app.route('/api/system/settings', methods=['GET'])
def get_system_settings():
    try:
        settings = config.system_settings
        return jsonify({'success': True, 'settings': settings})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取系统配置失败: {str(e)}'})

# 获取当前API Key
@app.route('/api/get_api_key', methods=['GET'])
def get_api_key():
    try:
        # 从key.txt文件读取API Key
        with open('key.txt', 'r', encoding='utf-8') as f:
            api_key = f.read().strip()
        return jsonify({'success': True, 'api_key': api_key})
    except FileNotFoundError:
        # 如果key.txt文件不存在，返回空字符串
        return jsonify({'success': True, 'api_key': ''})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取API Key失败: {str(e)}'})

# 更新API Key
@app.route('/api/update_api_key', methods=['POST'])
def update_api_key():
    try:
        data = request.get_json()
        new_api_key = data.get('api_key', '')
        if not new_api_key.strip():
            return jsonify({'success': False, 'message': 'API Key不能为空'})
        # 写入新的API Key到key.txt文件
        with open('key.txt', 'w', encoding='utf-8') as f:
            f.write(new_api_key.strip())
        # 更新全局API客户端
        global api_client, report_generator
        api_client = APIClient(new_api_key)
        report_generator = ReportGenerator(config, api_client)
        return jsonify({'success': True, 'message': 'API Key更新成功'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'更新API Key失败: {str(e)}'})

# 更新系统配置
@app.route('/api/system/settings', methods=['PUT'])
def update_system_settings():
    try:
        data = request.get_json()
        config.system_settings.update(data)
        config.save_system_settings()
        return jsonify({'success': True, 'message': '系统配置更新成功'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'更新系统配置失败: {str(e)}'})

# 工作流管理API

# 获取工作流列表
@app.route('/api/workflows', methods=['GET'])
def get_workflows():
    try:
        workflows = config.get_workflows()
        return jsonify({'success': True, 'workflows': workflows})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取工作流列表失败: {str(e)}'})

# 添加工作流
@app.route('/api/workflows', methods=['POST'])
def add_workflow():
    try:
        data = request.get_json()
        workflow = config.add_workflow(data)
        return jsonify({'success': True, 'workflow': workflow})
    except Exception as e:
        return jsonify({'success': False, 'message': f'添加工作流失败: {str(e)}'})

# 更新工作流
@app.route('/api/workflows/<workflow_id>', methods=['PUT'])
def update_workflow(workflow_id):
    try:
        data = request.get_json()
        workflow = config.update_workflow(workflow_id, data)
        if workflow:
            return jsonify({'success': True, 'workflow': workflow})
        else:
            return jsonify({'success': False, 'message': '工作流不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'更新工作流失败: {str(e)}'})

# 删除工作流
@app.route('/api/workflows/<workflow_id>', methods=['DELETE'])
def delete_workflow(workflow_id):
    try:
        config.delete_workflow(workflow_id)
        return jsonify({'success': True, 'message': '工作流删除成功'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'删除工作流失败: {str(e)}'})

# 测试Ollama连接
@app.route('/api/test_ollama_connection', methods=['POST'])
def test_ollama_connection():
    try:
        data = request.get_json()
        base_url = data.get('base_url', 'http://localhost:11434')
        model = data.get('model', 'llama3')
        
        # 创建临时API客户端测试连接
        from HOS_ME.utils.api_client import APIClient
        
        # 对于Ollama，API密钥可以是任意值
        test_client = APIClient(api_key='test', api_source='ollama')
        
        # 更新配置
        test_client.config['ollama']['base_url'] = base_url.replace('/v1/chat/completions', '')
        test_client.config['ollama']['model'] = model
        test_client.set_api_source('ollama')
        
        # 测试连接
        success, message = test_client.test_connection()
        
        return jsonify({'success': success, 'message': message})
    except Exception as e:
        return jsonify({'success': False, 'message': f'测试连接失败: {str(e)}'})

# 下载提示词导入模板
@app.route('/api/download_prompt_template', methods=['GET'])
def download_prompt_template():
    try:
        import os
        from flask import send_file
        
        template_path = os.path.join(os.getcwd(), 'prompt_import_template.xlsx')
        
        if os.path.exists(template_path):
            return send_file(template_path, as_attachment=True, download_name='提示词导入模板.xlsx')
        else:
            return jsonify({'success': False, 'message': '模板文件不存在'}), 404
    except Exception as e:
        return jsonify({'success': False, 'message': f'下载模板失败: {str(e)}'})

# AI生成工作流
@app.route('/api/workflows/generate', methods=['POST'])
def generate_workflow():
    try:
        data = request.get_json()
        prompt = data.get('prompt', '')
        
        if not prompt.trim():
            return jsonify({'success': False, 'message': '请输入工作流需求描述'})
        
        workflow = config.generate_workflow(prompt)
        return jsonify({'success': True, 'workflow': workflow})
    except Exception as e:
        return jsonify({'success': False, 'message': f'生成工作流失败: {str(e)}'})

# 将工作流转换为正式模块
@app.route('/api/workflows/<workflow_id>/convert_to_module', methods=['POST'])
def convert_workflow_to_module(workflow_id):
    try:
        module = config.convert_workflow_to_module(workflow_id)
        if module:
            return jsonify({'success': True, 'module': module, 'message': '工作流已成功转换为正式模块'})
        else:
            return jsonify({'success': False, 'message': '工作流不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'转换模块失败: {str(e)}'})

# 自定义模块管理API

# 获取自定义模块列表
@app.route('/api/modules', methods=['GET'])
def get_custom_modules():
    try:
        modules = config.get_custom_modules()
        return jsonify({'success': True, 'modules': modules})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取自定义模块列表失败: {str(e)}'})

# 添加自定义模块
@app.route('/api/modules', methods=['POST'])
def add_custom_module():
    try:
        data = request.get_json()
        module = config.add_custom_module(data)
        return jsonify({'success': True, 'module': module})
    except Exception as e:
        return jsonify({'success': False, 'message': f'添加自定义模块失败: {str(e)}'})

# 更新自定义模块
@app.route('/api/modules/<module_id>', methods=['PUT'])
def update_custom_module(module_id):
    try:
        data = request.get_json()
        module = config.update_custom_module(module_id, data)
        if module:
            return jsonify({'success': True, 'module': module})
        else:
            return jsonify({'success': False, 'message': '自定义模块不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'更新自定义模块失败: {str(e)}'})

# 删除自定义模块
@app.route('/api/modules/<module_id>', methods=['DELETE'])
def delete_custom_module(module_id):
    try:
        config.delete_custom_module(module_id)
        return jsonify({'success': True, 'message': '自定义模块删除成功'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'删除自定义模块失败: {str(e)}'})

# 重新排序自定义模块
@app.route('/api/modules/reorder', methods=['PUT'])
def reorder_custom_modules():
    try:
        data = request.get_json()
        module_ids = data.get('module_ids', [])
        config.reorder_custom_modules(module_ids)
        return jsonify({'success': True, 'message': '自定义模块排序更新成功'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'更新自定义模块排序失败: {str(e)}'})

# 工作流模板API

# 获取工作流模板列表
@app.route('/api/workflow-templates', methods=['GET'])
def get_workflow_templates():
    try:
        templates = config.hos_config.get('workflow_templates', [])
        return jsonify({'success': True, 'templates': templates})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取工作流模板列表失败: {str(e)}'})

# 获取指定工作流模板
@app.route('/api/workflow-templates/<template_id>', methods=['GET'])
def get_workflow_template(template_id):
    try:
        templates = config.hos_config.get('workflow_templates', [])
        template = next((t for t in templates if t['id'] == template_id), None)
        if template:
            return jsonify({'success': True, 'template': template})
        else:
            return jsonify({'success': False, 'message': '工作流模板不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取工作流模板失败: {str(e)}'})

# 批次管理API

# 获取所有批次
@app.route('/api/batches', methods=['GET'])
def get_batches():
    try:
        batches = report_generator.get_batches()
        return jsonify({'success': True, 'batches': batches})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取批次列表失败: {str(e)}'})

# 获取特定批次
@app.route('/api/batches/<batch_id>', methods=['GET'])
def get_batch(batch_id):
    try:
        batch = report_generator.get_batch(batch_id)
        if batch:
            return jsonify({'success': True, 'batch': batch})
        else:
            return jsonify({'success': False, 'message': '批次不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取批次失败: {str(e)}'})

# 批量下载API
@app.route('/api/batch_download', methods=['POST'])
def batch_download():
    try:
        data = request.get_json()
        filenames = data.get('filenames', [])
        batch_id = data.get('batch_id', None)
        
        # 处理批次下载
        if batch_id:
            batch = report_generator.get_batch(batch_id)
            if batch:
                filenames = batch['files']
        
        if not filenames:
            return jsonify({'success': False, 'message': '请选择要下载的文件'})
        
        # 创建临时目录
        import tempfile
        import zipfile
        import os
        
        # 创建临时目录
        temp_dir = tempfile.mkdtemp()
        
        # 创建压缩包
        zip_filename = f"batch_download_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.zip"
        zip_path = os.path.join(temp_dir, zip_filename)
        
        with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
            for filename in filenames:
                file_path = os.path.join(report_generator.config.reports_dir, filename)
                if os.path.exists(file_path):
                    zipf.write(file_path, filename)
        
        # 返回压缩包
        return send_file(zip_path, as_attachment=True, download_name=zip_filename)
    except Exception as e:
        return jsonify({'success': False, 'message': f'批量下载失败: {str(e)}'})

# 任务管理API

# 获取所有任务
@app.route('/api/tasks', methods=['GET'])
def get_tasks():
    try:
        tasks = app_task_manager.get_all_tasks()
        return jsonify({'success': True, 'tasks': tasks})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取任务列表失败: {str(e)}'})

# 获取特定任务
@app.route('/api/tasks/<task_id>', methods=['GET'])
def get_task(task_id):
    try:
        task = app_task_manager.get_task(task_id)
        if task:
            return jsonify({'success': True, 'task': task})
        else:
            return jsonify({'success': False, 'message': '任务不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取任务失败: {str(e)}'})

# 暂停任务
@app.route('/api/tasks/<task_id>/pause', methods=['POST'])
def pause_task(task_id):
    try:
        success = app_task_manager.pause_task(task_id)
        if success:
            return jsonify({'success': True, 'message': '任务已暂停'})
        else:
            return jsonify({'success': False, 'message': '任务不存在或无法暂停'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'暂停任务失败: {str(e)}'})

# 恢复任务
@app.route('/api/tasks/<task_id>/resume', methods=['POST'])
def resume_task(task_id):
    try:
        success = app_task_manager.resume_task(task_id)
        if success:
            return jsonify({'success': True, 'message': '任务已恢复'})
        else:
            return jsonify({'success': False, 'message': '任务不存在或无法恢复'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'恢复任务失败: {str(e)}'})

# 取消任务
@app.route('/api/tasks/<task_id>/cancel', methods=['POST'])
def cancel_task(task_id):
    try:
        success = app_task_manager.cancel_task(task_id)
        if success:
            return jsonify({'success': True, 'message': '任务已取消'})
        else:
            return jsonify({'success': False, 'message': '任务不存在或无法取消'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'取消任务失败: {str(e)}'})

# 清理已完成任务
@app.route('/api/tasks/cleanup', methods=['POST'])
def cleanup_tasks():
    try:
        app_task_manager.cleanup_completed_tasks()
        return jsonify({'success': True, 'message': '已清理已完成任务'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'清理任务失败: {str(e)}'})

# 批量提示词表格导入API
@app.route('/api/import_prompts_excel', methods=['POST'])
def import_prompts_excel():
    try:
        # 检查是否有文件上传
        if 'file' not in request.files:
            return jsonify({'success': False, 'message': '请选择要上传的Excel文件'})
        
        file = request.files['file']
        
        # 检查文件是否为空
        if file.filename == '':
            return jsonify({'success': False, 'message': '请选择要上传的Excel文件'})
        
        # 检查文件类型
        if not file.filename.endswith(('.xlsx', '.xls')):
            return jsonify({'success': False, 'message': '请选择Excel文件（.xlsx或.xls格式）'})
        
        # 导入pandas库
        import pandas as pd
        import io
        
        # 读取Excel文件
        df = pd.read_excel(io.BytesIO(file.read()))
        
        # 检查必要的列
        if 'prompt' not in df.columns:
            return jsonify({'success': False, 'message': 'Excel文件必须包含prompt列'})
        
        # 提取提示词列表
        prompts = []
        for index, row in df.iterrows():
            prompt = row['prompt']
            if not pd.isna(prompt) and str(prompt).strip():
                prompts.append(str(prompt).strip())
        
        return jsonify({
            'success': True, 
            'message': f'成功导入 {len(prompts)} 个提示词',
            'prompts': prompts
        })
    except Exception as e:
        return jsonify({'success': False, 'message': f'导入提示词失败: {str(e)}'})

# 工作流提示词模板API

# 获取工作流提示词模板列表
@app.route('/api/workflow-prompt-templates', methods=['GET'])
def get_workflow_prompt_templates():
    try:
        # 直接从文件加载工作流提示词模板
        import json
        import os
        workflow_prompt_templates_file = os.path.join(os.getcwd(), "workflow_prompt_templates.json")
        if os.path.exists(workflow_prompt_templates_file):
            with open(workflow_prompt_templates_file, "r", encoding="utf-8") as f:
                data = json.load(f)
                return jsonify({'success': True, 'templates': data.get('workflow_prompt_templates', [])})
        else:
            return jsonify({'success': True, 'templates': []})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取工作流提示词模板列表失败: {str(e)}'})

# 获取指定工作流提示词模板
@app.route('/api/workflow-prompt-templates/<template_id>', methods=['GET'])
def get_workflow_prompt_template(template_id):
    try:
        # 直接从文件加载工作流提示词模板
        import json
        import os
        workflow_prompt_templates_file = os.path.join(os.getcwd(), "workflow_prompt_templates.json")
        if os.path.exists(workflow_prompt_templates_file):
            with open(workflow_prompt_templates_file, "r", encoding="utf-8") as f:
                data = json.load(f)
                templates = data.get('workflow_prompt_templates', [])
                template = next((t for t in templates if t['id'] == template_id), None)
                if template:
                    return jsonify({'success': True, 'template': template})
                else:
                    return jsonify({'success': False, 'message': '工作流提示词模板不存在'})
        else:
            return jsonify({'success': False, 'message': '工作流提示词模板文件不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取工作流提示词模板失败: {str(e)}'})

# 会议管理API

# 获取会议列表
@app.route('/api/meetings', methods=['GET'])
def get_meetings():
    try:
        meetings = meeting_manager.get_meetings()
        return jsonify({'success': True, 'meetings': meetings})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取会议列表失败: {str(e)}'})

# 获取会议详情
@app.route('/api/meetings/<meeting_id>', methods=['GET'])
def get_meeting(meeting_id):
    try:
        meeting = meeting_manager.get_meeting(meeting_id)
        if meeting:
            return jsonify({'success': True, 'meeting': meeting})
        else:
            return jsonify({'success': False, 'message': '会议不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取会议详情失败: {str(e)}'})

# 创建会议
@app.route('/api/meetings', methods=['POST'])
def create_meeting():
    try:
        data = request.get_json()
        meeting = meeting_manager.create_meeting(data)
        return jsonify({'success': True, 'meeting': meeting})
    except Exception as e:
        return jsonify({'success': False, 'message': f'创建会议失败: {str(e)}'})

# 更新会议
@app.route('/api/meetings/<meeting_id>', methods=['PUT'])
def update_meeting(meeting_id):
    try:
        data = request.get_json()
        meeting = meeting_manager.update_meeting(meeting_id, data)
        if meeting:
            return jsonify({'success': True, 'meeting': meeting})
        else:
            return jsonify({'success': False, 'message': '会议不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'更新会议失败: {str(e)}'})

# 删除会议
@app.route('/api/meetings/<meeting_id>', methods=['DELETE'])
def delete_meeting(meeting_id):
    try:
        success = meeting_manager.delete_meeting(meeting_id)
        if success:
            return jsonify({'success': True, 'message': '删除会议成功'})
        else:
            return jsonify({'success': False, 'message': '会议不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'删除会议失败: {str(e)}'})

# 生成会议纪要
@app.route('/api/meetings/<meeting_id>/generate_minutes', methods=['POST'])
def generate_minutes(meeting_id):
    try:
        data = request.get_json()
        prompt = data.get('prompt', '')
        template_id = data.get('template_id', None)
        
        if not prompt.strip():
            return jsonify({'success': False, 'message': '请输入提示词'})
        
        minutes = meeting_manager.generate_minutes(meeting_id, prompt, template_id)
        return jsonify({'success': True, 'minutes': minutes})
    except Exception as e:
        return jsonify({'success': False, 'message': f'生成会议纪要失败: {str(e)}'})

# 添加行动项
@app.route('/api/meetings/<meeting_id>/action_items', methods=['POST'])
def add_action_item(meeting_id):
    try:
        data = request.get_json()
        action_item = meeting_manager.add_action_item(meeting_id, data)
        if action_item:
            return jsonify({'success': True, 'action_item': action_item})
        else:
            return jsonify({'success': False, 'message': '会议不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'添加行动项失败: {str(e)}'})

# 更新行动项
@app.route('/api/meetings/<meeting_id>/action_items/<action_item_id>', methods=['PUT'])
def update_action_item(meeting_id, action_item_id):
    try:
        data = request.get_json()
        action_item = meeting_manager.update_action_item(meeting_id, action_item_id, data)
        if action_item:
            return jsonify({'success': True, 'action_item': action_item})
        else:
            return jsonify({'success': False, 'message': '行动项不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'更新行动项失败: {str(e)}'})

# 删除行动项
@app.route('/api/meetings/<meeting_id>/action_items/<action_item_id>', methods=['DELETE'])
def delete_action_item(meeting_id, action_item_id):
    try:
        success = meeting_manager.delete_action_item(meeting_id, action_item_id)
        if success:
            return jsonify({'success': True, 'message': '删除行动项成功'})
        else:
            return jsonify({'success': False, 'message': '行动项不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'删除行动项失败: {str(e)}'})

# 会议管理扩展API

# 添加参会人员
@app.route('/api/meetings/<meeting_id>/attendees', methods=['POST'])
def add_attendee(meeting_id):
    try:
        data = request.get_json()
        attendee = data.get('attendee', '')
        if not attendee:
            return jsonify({'success': False, 'message': '参会人员不能为空'})
        meeting = meeting_manager.add_attendee(meeting_id, attendee)
        if meeting:
            return jsonify({'success': True, 'meeting': meeting})
        else:
            return jsonify({'success': False, 'message': '会议不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'添加参会人员失败: {str(e)}'})

# 移除参会人员
@app.route('/api/meetings/<meeting_id>/attendees', methods=['DELETE'])
def remove_attendee(meeting_id):
    try:
        data = request.get_json()
        attendee = data.get('attendee', '')
        if not attendee:
            return jsonify({'success': False, 'message': '参会人员不能为空'})
        meeting = meeting_manager.remove_attendee(meeting_id, attendee)
        if meeting:
            return jsonify({'success': True, 'meeting': meeting})
        else:
            return jsonify({'success': False, 'message': '会议不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'移除参会人员失败: {str(e)}'})

# 设置会议提醒
@app.route('/api/meetings/<meeting_id>/reminder', methods=['PUT'])
def set_meeting_reminder(meeting_id):
    try:
        data = request.get_json()
        reminder_time = data.get('reminder_time', '')
        if not reminder_time:
            return jsonify({'success': False, 'message': '提醒时间不能为空'})
        meeting = meeting_manager.set_meeting_reminder(meeting_id, reminder_time)
        if meeting:
            return jsonify({'success': True, 'meeting': meeting})
        else:
            return jsonify({'success': False, 'message': '会议不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'设置会议提醒失败: {str(e)}'})

# 获取即将到来的会议
@app.route('/api/meetings/upcoming', methods=['GET'])
def get_upcoming_meetings():
    try:
        days = request.args.get('days', 7)
        meetings = meeting_manager.get_upcoming_meetings(int(days))
        return jsonify({'success': True, 'meetings': meetings})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取即将到来的会议失败: {str(e)}'})

# 生成会议报告
@app.route('/api/meetings/<meeting_id>/report', methods=['GET'])
def generate_meeting_report(meeting_id):
    try:
        report = meeting_manager.generate_meeting_report(meeting_id)
        if report:
            return jsonify({'success': True, 'report': report})
        else:
            return jsonify({'success': False, 'message': '会议不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'生成会议报告失败: {str(e)}'})

# Ollama管理API
import subprocess
import platform
import os
import tempfile
import json

@app.route('/api/ollama/status', methods=['GET'])
def get_ollama_status():
    """检查Ollama安装状态"""
    try:
        # 检查Ollama是否已安装
        result = subprocess.run(['ollama', '--version'], capture_output=True, text=True)
        if result.returncode == 0:
            # Ollama已安装，检查服务是否正在运行
            if platform.system() == 'Windows':
                # Windows系统
                result = subprocess.run(['net', 'start'], capture_output=True, text=True)
                if 'ollama' in result.stdout.lower():
                    status = 'running'
                else:
                    status = 'installed'
            else:
                # Linux/macOS系统
                result = subprocess.run(['pgrep', 'ollama'], capture_output=True, text=True)
                if result.stdout.strip():
                    status = 'running'
                else:
                    status = 'installed'
            
            return jsonify({
                'success': True,
                'status': status,
                'version': result.stdout.strip()
            })
        else:
            # Ollama未安装
            return jsonify({
                'success': True,
                'status': 'not_installed',
                'message': 'Ollama未安装'
            })
    except FileNotFoundError:
        # ollama命令不存在
        return jsonify({
            'success': True,
            'status': 'not_installed',
            'message': 'Ollama未安装'
        })
    except Exception as e:
        return jsonify({
            'success': False,
            'message': f'检查Ollama状态失败: {str(e)}'
        })

@app.route('/api/ollama/install', methods=['POST'])
def install_ollama():
    """安装Ollama服务"""
    try:
        system = platform.system()
        
        if system == 'Windows':
            # Windows系统：使用winget安装
            cmd = ['winget', 'install', 'ollama', '--accept-source-agreements', '--accept-package-agreements']
        elif system == 'Darwin':
            # macOS系统：使用brew安装
            cmd = ['brew', 'install', 'ollama']
        elif system == 'Linux':
            # Linux系统：使用脚本安装
            cmd = ['bash', '-c', 'curl -fsSL https://ollama.com/install.sh | sh']
        else:
            return jsonify({
                'success': False,
                'message': f'不支持的操作系统: {system}'
            })
        
        # 执行安装命令
        process = subprocess.Popen(
            cmd,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
            text=True
        )
        
        # 读取安装输出
        output = ''
        for line in process.stdout:
            output += line
        
        process.wait()
        
        if process.returncode == 0:
            return jsonify({
                'success': True,
                'message': 'Ollama安装成功',
                'output': output
            })
        else:
            return jsonify({
                'success': False,
                'message': f'Ollama安装失败: {output}'
            })
    except Exception as e:
        return jsonify({
            'success': False,
            'message': f'安装Ollama失败: {str(e)}'
        })

@app.route('/api/ollama/start', methods=['POST'])
def start_ollama():
    """启动Ollama服务"""
    try:
        system = platform.system()
        
        if system == 'Windows':
            # Windows系统：启动服务
            cmd = ['net', 'start', 'ollama']
        elif system == 'Darwin':
            # macOS系统：使用launchctl启动
            cmd = ['brew', 'services', 'start', 'ollama']
        elif system == 'Linux':
            # Linux系统：使用systemctl启动
            cmd = ['systemctl', 'start', 'ollama']
        else:
            return jsonify({
                'success': False,
                'message': f'不支持的操作系统: {system}'
            })
        
        # 执行启动命令
        result = subprocess.run(cmd, capture_output=True, text=True)
        
        if result.returncode == 0:
            return jsonify({
                'success': True,
                'message': 'Ollama服务启动成功'
            })
        else:
            return jsonify({
                'success': False,
                'message': f'Ollama服务启动失败: {result.stderr}'
            })
    except Exception as e:
        return jsonify({
            'success': False,
            'message': f'启动Ollama服务失败: {str(e)}'
        })

@app.route('/api/ollama/stop', methods=['POST'])
def stop_ollama():
    """停止Ollama服务"""
    try:
        system = platform.system()
        
        if system == 'Windows':
            # Windows系统：停止服务
            cmd = ['net', 'stop', 'ollama']
        elif system == 'Darwin':
            # macOS系统：使用launchctl停止
            cmd = ['brew', 'services', 'stop', 'ollama']
        elif system == 'Linux':
            # Linux系统：使用systemctl停止
            cmd = ['systemctl', 'stop', 'ollama']
        else:
            return jsonify({
                'success': False,
                'message': f'不支持的操作系统: {system}'
            })
        
        # 执行停止命令
        result = subprocess.run(cmd, capture_output=True, text=True)
        
        if result.returncode == 0:
            return jsonify({
                'success': True,
                'message': 'Ollama服务停止成功'
            })
        else:
            return jsonify({
                'success': False,
                'message': f'Ollama服务停止失败: {result.stderr}'
            })
    except Exception as e:
        return jsonify({
            'success': False,
            'message': f'停止Ollama服务失败: {str(e)}'
        })

@app.route('/api/ollama/models/available', methods=['GET'])
def get_available_models():
    """获取可用模型列表"""
    try:
        # 使用ollama list命令获取可用模型
        result = subprocess.run(['ollama', 'list'], capture_output=True, text=True)
        
        if result.returncode == 0:
            # 解析输出
            lines = result.stdout.strip().split('\n')
            models = []
            for line in lines[1:]:  # 跳过标题行
                if line.strip():
                    parts = line.split()
                    if len(parts) >= 3:
                        model = {
                            'name': parts[0],
                            'id': parts[0],
                            'size': parts[1],
                            'modified': parts[2]
                        }
                        models.append(model)
            
            # 添加推荐模型（A3B Q8量化）
            recommended_models = [
                {
                    'name': 'a3b',
                    'id': 'a3b',
                    'size': '~3.1GB',
                    'description': 'A3B模型（推荐Q8量化）',
                    'is_recommended': True,
                    'quantization': 'Q8',
                    'popular': True
                }
            ]
            
            return jsonify({
                'success': True,
                'models': recommended_models + models
            })
        else:
            return jsonify({
                'success': False,
                'message': f'获取可用模型失败: {result.stderr}'
            })
    except Exception as e:
        return jsonify({
            'success': False,
            'message': f'获取可用模型失败: {str(e)}'
        })

@app.route('/api/ollama/models/installed', methods=['GET'])
def get_installed_models():
    """获取已安装模型列表"""
    try:
        # 使用ollama list命令获取已安装模型
        result = subprocess.run(['ollama', 'list'], capture_output=True, text=True)
        
        if result.returncode == 0:
            # 解析输出
            lines = result.stdout.strip().split('\n')
            models = []
            for line in lines[1:]:  # 跳过标题行
                if line.strip():
                    parts = line.split()
                    if len(parts) >= 3:
                        model = {
                            'name': parts[0],
                            'id': parts[0],
                            'size': parts[1],
                            'modified': parts[2]
                        }
                        models.append(model)
            
            return jsonify({
                'success': True,
                'models': models
            })
        else:
            return jsonify({
                'success': False,
                'message': f'获取已安装模型失败: {result.stderr}'
            })
    except Exception as e:
        return jsonify({
            'success': False,
            'message': f'获取已安装模型失败: {str(e)}'
        })

@app.route('/api/ollama/models/download', methods=['POST'])
def download_model():
    """下载指定模型"""
    try:
        data = request.get_json()
        model_name = data.get('model_name', '')
        quantization = data.get('quantization', '')
        
        if not model_name:
            return jsonify({
                'success': False,
                'message': '请指定要下载的模型名称'
            })
        
        # 构建完整模型名称（包含量化级别）
        full_model_name = model_name
        if quantization:
            full_model_name += f':{quantization}'
        
        # 使用ollama pull命令下载模型
        process = subprocess.Popen(
            ['ollama', 'pull', full_model_name],
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
            text=True
        )
        
        # 读取并返回下载进度
        output = ''
        for line in process.stdout:
            output += line
        
        process.wait()
        
        if process.returncode == 0:
            return jsonify({
                'success': True,
                'message': f'模型 {full_model_name} 下载成功',
                'output': output
            })
        else:
            return jsonify({
                'success': False,
                'message': f'模型下载失败: {output}'
            })
    except Exception as e:
        return jsonify({
            'success': False,
            'message': f'下载模型失败: {str(e)}'
        })

@app.route('/api/ollama/models/delete', methods=['POST'])
def delete_model():
    """删除指定模型"""
    try:
        data = request.get_json()
        model_name = data.get('model_name', '')
        
        if not model_name:
            return jsonify({
                'success': False,
                'message': '请指定要删除的模型名称'
            })
        
        # 使用ollama rm命令删除模型
        result = subprocess.run(
            ['ollama', 'rm', model_name],
            capture_output=True,
            text=True
        )
        
        if result.returncode == 0:
            return jsonify({
                'success': True,
                'message': f'模型 {model_name} 删除成功'
            })
        else:
            return jsonify({
                'success': False,
                'message': f'删除模型失败: {result.stderr}'
            })
    except Exception as e:
        return jsonify({
            'success': False,
            'message': f'删除模型失败: {str(e)}'
        })

@app.route('/api/ollama/models/set-default', methods=['POST'])
def set_default_model():
    """设置默认模型"""
    try:
        data = request.get_json()
        model_name = data.get('model_name', '')
        
        if not model_name:
            return jsonify({
                'success': False,
                'message': '请指定要设置的默认模型名称'
            })
        
        # 更新Ollama配置，设置默认模型
        # 注意：Ollama当前不支持直接设置默认模型，需要更新配置文件
        
        # 找到Ollama配置文件
        if platform.system() == 'Windows':
            config_path = os.path.expanduser('~\.ollama\config.json')
        else:
            config_path = os.path.expanduser('~/.ollama/config.json')
        
        # 读取现有配置
        if os.path.exists(config_path):
            with open(config_path, 'r') as f:
                config = json.load(f)
        else:
            config = {}
        
        # 更新默认模型
        config['default'] = model_name
        
        # 保存配置
        with open(config_path, 'w') as f:
            json.dump(config, f, indent=2)
        
        return jsonify({
            'success': True,
            'message': f'默认模型已设置为 {model_name}'
        })
    except Exception as e:
        return jsonify({
            'success': False,
            'message': f'设置默认模型失败: {str(e)}'
        })

# 获取会议统计信息
@app.route('/api/meetings/statistics', methods=['GET'])
def get_meeting_statistics():
    try:
        statistics = meeting_manager.get_meeting_statistics()
        return jsonify({'success': True, 'statistics': statistics})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取会议统计信息失败: {str(e)}'})

# 项目管理API

# 获取项目列表
@app.route('/api/projects', methods=['GET'])
def get_projects():
    try:
        projects = project_manager.get_projects()
        return jsonify({'success': True, 'projects': projects})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取项目列表失败: {str(e)}'})

# 文档转换API

# 获取支持的转换格式
@app.route('/api/document-converter/formats', methods=['GET'])
def get_supported_formats():
    try:
        formats = document_converter.get_supported_formats()
        return jsonify({
            'success': True,
            'formats': formats,
            'pandoc_available': document_converter.is_available(),
            'pandoc_version': document_converter.get_pandoc_version()
        })
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取支持的格式失败: {str(e)}'})

# 获取支持的Pandoc扩展
@app.route('/api/document-converter/extensions', methods=['GET'])
def get_supported_extensions():
    try:
        extensions = document_converter.get_supported_extensions()
        return jsonify({
            'success': True,
            'extensions': extensions
        })
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取支持的扩展失败: {str(e)}'})

# 批量转换文件
@app.route('/api/document-converter/batch-convert', methods=['POST'])
def batch_convert():
    try:
        import tempfile
        import zipfile
        import os
        
        # 检查是否有文件上传
        if 'files' not in request.files:
            return jsonify({'success': False, 'message': '请选择要上传的文件'})
        
        files = request.files.getlist('files')
        to_format = request.form.get('to_format', 'html')
        options = request.form.get('options', '{}')
        
        # 创建临时目录
        temp_dir = tempfile.mkdtemp()
        input_files = []
        
        # 保存上传的文件
        for file in files:
            if file.filename:
                temp_file_path = os.path.join(temp_dir, file.filename)
                file.save(temp_file_path)
                input_files.append(temp_file_path)
        
        # 执行批量转换
        result = document_converter.batch_convert(input_files, temp_dir, to_format, json.loads(options))
        
        if result['success']:
            # 创建压缩包
            zip_filename = f"batch_convert_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.zip"
            zip_path = os.path.join(temp_dir, zip_filename)
            
            with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
                for input_file in input_files:
                    base_name = os.path.splitext(os.path.basename(input_file))[0]
                    output_file = os.path.join(temp_dir, f'{base_name}.{to_format}')
                    if os.path.exists(output_file):
                        zipf.write(output_file, f'{base_name}.{to_format}')
            
            # 返回压缩包
            response = send_file(zip_path, as_attachment=True, download_name=zip_filename)
            
            # 添加清理函数
            @response.call_on_close
            def cleanup():
                try:
                    import shutil
                    shutil.rmtree(temp_dir)
                except:
                    pass
            
            return response
        else:
            # 清理临时文件
            import shutil
            shutil.rmtree(temp_dir)
            return jsonify(result)
    except Exception as e:
        return jsonify({'success': False, 'message': f'批量转换失败: {str(e)}'})

# 创建复杂表格
@app.route('/api/document-converter/create-complex-table', methods=['POST'])
def create_complex_table():
    try:
        data = request.get_json()
        table_data = data.get('data', [])
        headers = data.get('headers', [])
        rowspan = data.get('rowspan', None)
        colspan = data.get('colspan', None)
        
        if not table_data or not headers:
            return jsonify({'success': False, 'message': '表格数据和表头不能为空'})
        
        table_html = document_converter.create_complex_table(table_data, headers, rowspan, colspan)
        
        return jsonify({
            'success': True,
            'table_html': table_html,
            'message': '复杂表格创建成功'
        })
    except Exception as e:
        return jsonify({'success': False, 'message': f'创建复杂表格失败: {str(e)}'})

# 优化HTML内容
@app.route('/api/document-converter/optimize-html', methods=['POST'])
def optimize_html():
    try:
        data = request.get_json()
        html_content = data.get('html_content', '')
        options = data.get('options', {})
        
        if not html_content.strip():
            return jsonify({'success': False, 'message': 'HTML内容不能为空'})
        
        result = document_converter.optimize_for_web(html_content, options)
        return jsonify(result)
    except Exception as e:
        return jsonify({'success': False, 'message': f'优化HTML内容失败: {str(e)}'})

# 转换文档内容
@app.route('/api/document-converter/convert', methods=['POST'])
def convert_document():
    try:
        data = request.get_json()
        input_content = data.get('input_content', '')
        from_format = data.get('from_format', 'markdown')
        to_format = data.get('to_format', 'html')
        options = data.get('options', {})
        
        # 确保options是字典格式
        if isinstance(options, list):
            # 兼容旧的列表格式，将其转换为新的字典格式
            options = {'pandoc_args': options}
        
        result = document_converter.convert(input_content, from_format, to_format, options)
        return jsonify(result)
    except Exception as e:
        return jsonify({'success': False, 'message': f'转换文档失败: {str(e)}'})

# 转换文件
@app.route('/api/document-converter/convert-file', methods=['POST'])
def convert_file():
    try:
        if 'file' not in request.files:
            return jsonify({'success': False, 'message': '请选择要转换的文件'})
        
        file = request.files['file']
        to_format = request.form.get('to_format', 'html')
        options = request.form.get('options', '{}')
        
        # 保存上传的文件到临时位置
        import tempfile
        temp_file_path = os.path.join(tempfile.gettempdir(), file.filename)
        file.save(temp_file_path)
        
        # 生成输出文件名
        output_filename = f'{os.path.splitext(file.filename)[0]}.{to_format}'
        output_file_path = os.path.join(tempfile.gettempdir(), output_filename)
        
        # 执行转换
        result = document_converter.convert_file(temp_file_path, output_file_path, json.loads(options))
        
        # 删除临时输入文件
        os.unlink(temp_file_path)
        
        if result['success']:
            # 返回转换后的文件
            response = send_file(output_file_path, as_attachment=True, download_name=output_filename)
            # 添加清理函数，在响应完成后删除临时输出文件
            @response.call_on_close
            def cleanup():
                try:
                    os.unlink(output_file_path)
                except:
                    pass
            return response
        else:
            # 删除临时输出文件
            if os.path.exists(output_file_path):
                os.unlink(output_file_path)
            return jsonify(result)
    except Exception as e:
        return jsonify({'success': False, 'message': f'转换文件失败: {str(e)}'})

# 渲染图表
@app.route('/api/kroki/render', methods=['POST'])
def render_chart():
    try:
        data = request.get_json()
        diagram_type = data.get('diagram_type', 'mermaid')
        chart_code = data.get('chart_code', '')
        output_format = data.get('output_format', 'svg')
        options = data.get('options', {})
        
        result = kroki_renderer.render_chart(diagram_type, chart_code, output_format, options)
        
        if result['success']:
            # 返回渲染后的图表
            return send_file(result['file_path'], mimetype=f'image/{output_format}', as_attachment=True)
        else:
            return jsonify(result)
    except Exception as e:
        return jsonify({'success': False, 'message': f'渲染图表失败: {str(e)}'})

# 渲染文本中的图表
@app.route('/api/kroki/render-text', methods=['POST'])
def render_text_with_charts():
    try:
        data = request.get_json()
        text_content = data.get('text_content', '')
        output_format = data.get('output_format', 'svg')
        options = data.get('options', {})
        
        # 创建临时输出目录
        import tempfile
        output_dir = tempfile.mkdtemp()
        
        result = kroki_renderer.render_text_with_charts(text_content, output_dir, output_format, options)
        
        return jsonify(result)
    except Exception as e:
        return jsonify({'success': False, 'message': f'渲染文本中的图表失败: {str(e)}'})

# 获取支持的图表类型
@app.route('/api/kroki/diagrams', methods=['GET'])
def get_supported_diagrams():
    try:
        diagrams = kroki_renderer.get_supported_diagrams()
        output_formats = kroki_renderer.get_supported_output_formats()
        return jsonify({
            'success': True,
            'diagrams': diagrams,
            'output_formats': output_formats
        })
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取支持的图表类型失败: {str(e)}'})

# 转换ASCII图像
@app.route('/api/ascii-converter/convert', methods=['POST'])
def convert_ascii_image():
    try:
        data = request.get_json()
        ascii_content = data.get('ascii_content', '')
        options = data.get('options', {})
        
        # 创建临时输出目录
        import tempfile
        output_dir = tempfile.mkdtemp()
        output_path = os.path.join(output_dir, 'ascii_image.png')
        
        result = ascii_converter.ascii_to_image(ascii_content, output_path, options)
        
        if result['success']:
            # 返回转换后的图像
            return send_file(result['file_path'], mimetype='image/png', as_attachment=True)
        else:
            return jsonify(result)
    except Exception as e:
        return jsonify({'success': False, 'message': f'转换ASCII图像失败: {str(e)}'})

# 转换文本中的ASCII图像
@app.route('/api/ascii-converter/convert-text', methods=['POST'])
def convert_text_with_ascii():
    try:
        data = request.get_json()
        text_content = data.get('text_content', '')
        options = data.get('options', {})
        
        # 创建临时输出目录
        import tempfile
        output_dir = tempfile.mkdtemp()
        
        result = ascii_converter.convert_text_with_ascii(text_content, output_dir, options)
        
        return jsonify(result)
    except Exception as e:
        return jsonify({'success': False, 'message': f'转换文本中的ASCII图像失败: {str(e)}'})

# 批量处理文档
@app.route('/api/batch-processor/process', methods=['POST'])
def batch_process_documents():
    try:
        data = request.get_json()
        input_files = data.get('input_files', [])
        conversion_config = data.get('conversion_config', {})
        
        result = batch_processor.process_batch(input_files, conversion_config)
        return jsonify(result)
    except Exception as e:
        return jsonify({'success': False, 'message': f'批量处理文档失败: {str(e)}'})

# 批量处理文本
@app.route('/api/batch-processor/process-text', methods=['POST'])
def batch_process_text():
    try:
        data = request.get_json()
        texts = data.get('texts', [])
        conversion_config = data.get('conversion_config', {})
        
        result = batch_processor.process_text_batch(texts, conversion_config)
        return jsonify(result)
    except Exception as e:
        return jsonify({'success': False, 'message': f'批量处理文本失败: {str(e)}'})

# 获取批量处理进度
@app.route('/api/batch-processor/progress', methods=['GET'])
def get_batch_progress():
    try:
        progress = batch_processor.get_progress()
        return jsonify({'success': True, 'progress': progress})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取批量处理进度失败: {str(e)}'})

# 取消批量处理
@app.route('/api/batch-processor/cancel', methods=['POST'])
def cancel_batch_processing():
    try:
        result = batch_processor.cancel_processing()
        return jsonify(result)
    except Exception as e:
        return jsonify({'success': False, 'message': f'取消批量处理失败: {str(e)}'})

# 获取项目详情
@app.route('/api/projects/<project_id>', methods=['GET'])
def get_project(project_id):
    try:
        project = project_manager.get_project(project_id)
        if project:
            return jsonify({'success': True, 'project': project})
        else:
            return jsonify({'success': False, 'message': '项目不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取项目详情失败: {str(e)}'})

# 创建项目
@app.route('/api/projects', methods=['POST'])
def create_project():
    try:
        data = request.get_json()
        project = project_manager.create_project(data)
        return jsonify({'success': True, 'project': project})
    except Exception as e:
        return jsonify({'success': False, 'message': f'创建项目失败: {str(e)}'})

# 更新项目
@app.route('/api/projects/<project_id>', methods=['PUT'])
def update_project(project_id):
    try:
        data = request.get_json()
        project = project_manager.update_project(project_id, data)
        if project:
            return jsonify({'success': True, 'project': project})
        else:
            return jsonify({'success': False, 'message': '项目不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'更新项目失败: {str(e)}'})

# 删除项目
@app.route('/api/projects/<project_id>', methods=['DELETE'])
def delete_project(project_id):
    try:
        success = project_manager.delete_project(project_id)
        if success:
            return jsonify({'success': True, 'message': '删除项目成功'})
        else:
            return jsonify({'success': False, 'message': '项目不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'删除项目失败: {str(e)}'})

# 获取项目任务列表
@app.route('/api/projects/<project_id>/tasks', methods=['GET'])
def get_project_tasks(project_id):
    try:
        tasks = project_manager.get_tasks(project_id)
        return jsonify({'success': True, 'tasks': tasks})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取任务列表失败: {str(e)}'})



# 添加项目文档
@app.route('/api/projects/<project_id>/documents', methods=['POST'])
def add_document(project_id):
    try:
        data = request.get_json()
        document = project_manager.add_document(project_id, data)
        if document:
            return jsonify({'success': True, 'document': document})
        else:
            return jsonify({'success': False, 'message': '项目不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'添加文档失败: {str(e)}'})

# 删除项目文档
@app.route('/api/projects/<project_id>/documents/<document_id>', methods=['DELETE'])
def delete_document(project_id, document_id):
    try:
        success = project_manager.remove_document(project_id, document_id)
        if success:
            return jsonify({'success': True, 'message': '删除文档成功'})
        else:
            return jsonify({'success': False, 'message': '文档不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'删除文档失败: {str(e)}'})

# 项目管理扩展API

# 更新项目进度
@app.route('/api/projects/<project_id>/progress', methods=['PUT'])
def update_project_progress(project_id):
    try:
        data = request.get_json()
        progress = data.get('progress', 0)
        project = project_manager.update_project_progress(project_id, progress)
        if project:
            return jsonify({'success': True, 'project': project})
        else:
            return jsonify({'success': False, 'message': '项目不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'更新项目进度失败: {str(e)}'})

# 获取项目甘特图数据
@app.route('/api/projects/<project_id>/gantt', methods=['GET'])
def get_project_gantt_data(project_id):
    try:
        gantt_data = project_manager.get_project_gantt_data(project_id)
        if gantt_data:
            return jsonify({'success': True, 'data': gantt_data})
        else:
            return jsonify({'success': False, 'message': '项目不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取甘特图数据失败: {str(e)}'})

# 生成项目报告
@app.route('/api/projects/<project_id>/report', methods=['GET'])
def generate_project_report(project_id):
    try:
        report = project_manager.generate_project_report(project_id)
        if report:
            return jsonify({'success': True, 'report': report})
        else:
            return jsonify({'success': False, 'message': '项目不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'生成项目报告失败: {str(e)}'})

# 获取项目统计信息
@app.route('/api/projects/statistics', methods=['GET'])
def get_project_statistics():
    try:
        statistics = project_manager.get_project_statistics()
        return jsonify({'success': True, 'statistics': statistics})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取项目统计信息失败: {str(e)}'})

# 添加项目里程碑
@app.route('/api/projects/<project_id>/milestones', methods=['POST'])
def add_project_milestone(project_id):
    try:
        data = request.get_json()
        milestone = project_manager.add_project_milestone(project_id, data)
        if milestone:
            return jsonify({'success': True, 'project': milestone})
        else:
            return jsonify({'success': False, 'message': '项目不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'添加项目里程碑失败: {str(e)}'})

# 更新项目里程碑
@app.route('/api/projects/<project_id>/milestones/<milestone_id>', methods=['PUT'])
def update_project_milestone(project_id, milestone_id):
    try:
        data = request.get_json()
        milestone = project_manager.update_project_milestone(project_id, milestone_id, data)
        if milestone:
            return jsonify({'success': True, 'project': milestone})
        else:
            return jsonify({'success': False, 'message': '项目或里程碑不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'更新项目里程碑失败: {str(e)}'})

# 删除项目里程碑
@app.route('/api/projects/<project_id>/milestones/<milestone_id>', methods=['DELETE'])
def delete_project_milestone(project_id, milestone_id):
    try:
        milestone = project_manager.delete_project_milestone(project_id, milestone_id)
        if milestone:
            return jsonify({'success': True, 'project': milestone})
        else:
            return jsonify({'success': False, 'message': '项目或里程碑不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'删除项目里程碑失败: {str(e)}'})

# RAG库管理API

# 获取所有RAG库
@app.route('/api/rag/libraries', methods=['GET'])
def get_rag_libraries():
    try:
        libraries = knowledge_base.get_rag_libraries()
        return jsonify({'success': True, 'libraries': libraries})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取RAG库列表失败: {str(e)}'})

# 获取指定RAG库
@app.route('/api/rag/libraries/<library_id>', methods=['GET'])
def get_rag_library(library_id):
    try:
        library = knowledge_base.get_rag_library(library_id)
        if library:
            return jsonify({'success': True, 'library': library})
        else:
            return jsonify({'success': False, 'message': 'RAG库不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取RAG库失败: {str(e)}'})

# 创建RAG库
@app.route('/api/rag/libraries', methods=['POST'])
def create_rag_library():
    try:
        data = request.get_json()
        library = knowledge_base.create_rag_library(data)
        return jsonify({'success': True, 'library': library})
    except Exception as e:
        return jsonify({'success': False, 'message': f'创建RAG库失败: {str(e)}'})

# 更新RAG库
@app.route('/api/rag/libraries/<library_id>', methods=['PUT'])
def update_rag_library(library_id):
    try:
        data = request.get_json()
        library = knowledge_base.update_rag_library(library_id, data)
        if library:
            return jsonify({'success': True, 'library': library})
        else:
            return jsonify({'success': False, 'message': 'RAG库不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'更新RAG库失败: {str(e)}'})

# 删除RAG库
@app.route('/api/rag/libraries/<library_id>', methods=['DELETE'])
def delete_rag_library(library_id):
    try:
        success = knowledge_base.delete_rag_library(library_id)
        if success:
            return jsonify({'success': True, 'message': 'RAG库删除成功'})
        else:
            return jsonify({'success': False, 'message': 'RAG库不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'删除RAG库失败: {str(e)}'})

# 获取RAG库文档
@app.route('/api/rag/libraries/<library_id>/documents', methods=['GET'])
def get_rag_library_documents(library_id):
    try:
        documents = knowledge_base.get_rag_library_documents(library_id)
        return jsonify({'success': True, 'documents': documents})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取RAG库文档失败: {str(e)}'})

# 添加文档到RAG库
@app.route('/api/rag/libraries/<library_id>/documents', methods=['POST'])
def add_document_to_rag_library(library_id):
    try:
        data = request.get_json()
        document_id = data.get('document_id')
        if not document_id:
            return jsonify({'success': False, 'message': '文档ID不能为空'})
        success = knowledge_base.add_document_to_rag_library(library_id, document_id)
        if success:
            return jsonify({'success': True, 'message': '文档添加成功'})
        else:
            return jsonify({'success': False, 'message': '添加失败，RAG库或文档不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'添加文档到RAG库失败: {str(e)}'})

# 从RAG库移除文档
@app.route('/api/rag/libraries/<library_id>/documents/<document_id>', methods=['DELETE'])
def remove_document_from_rag_library(library_id, document_id):
    try:
        success = knowledge_base.remove_document_from_rag_library(library_id, document_id)
        if success:
            return jsonify({'success': True, 'message': '文档移除成功'})
        else:
            return jsonify({'success': False, 'message': '移除失败，RAG库或文档不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'从RAG库移除文档失败: {str(e)}'})

# 生成RAG库所有文档嵌入
@app.route('/api/rag/libraries/<library_id>/generate-embeddings', methods=['POST'])
def generate_rag_library_embeddings(library_id):
    try:
        success = knowledge_base.generate_all_embeddings(library_id)
        if success:
            return jsonify({'success': True, 'message': '嵌入生成成功'})
        else:
            return jsonify({'success': False, 'message': 'RAG库不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'生成嵌入失败: {str(e)}'})

# RAG查询
@app.route('/api/rag/query', methods=['POST'])
def rag_query():
    try:
        data = request.get_json()
        query = data.get('query')
        library_id = data.get('library_id')
        top_k = data.get('top_k', 5)
        
        if not query or not library_id:
            return jsonify({'success': False, 'message': '查询内容和RAG库ID不能为空'})
        
        results = knowledge_base.rag_query(query, library_id, top_k)
        return jsonify({'success': True, 'results': results})
    except Exception as e:
        return jsonify({'success': False, 'message': f'RAG查询失败: {str(e)}'})

# 批量RAG查询
@app.route('/api/rag/batch-query', methods=['POST'])
def batch_rag_query():
    try:
        data = request.get_json()
        queries = data.get('queries', [])
        library_id = data.get('library_id')
        top_k = data.get('top_k', 3)
        
        if not queries or not library_id:
            return jsonify({'success': False, 'message': '查询列表和RAG库ID不能为空'})
        
        results = knowledge_base.batch_rag_query(queries, library_id, top_k)
        return jsonify({'success': True, 'results': results})
    except Exception as e:
        return jsonify({'success': False, 'message': f'批量RAG查询失败: {str(e)}'})

# 获取RAG库统计信息
@app.route('/api/rag/libraries/<library_id>/statistics', methods=['GET'])
def get_rag_library_statistics(library_id):
    try:
        statistics = knowledge_base.get_rag_library_statistics(library_id)
        if statistics:
            return jsonify({'success': True, 'statistics': statistics})
        else:
            return jsonify({'success': False, 'message': 'RAG库不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取RAG库统计信息失败: {str(e)}'})

# 搜索RAG库
@app.route('/api/rag/libraries/search', methods=['GET'])
def search_rag_libraries():
    try:
        query = request.args.get('q', '')
        results = knowledge_base.search_rag_libraries(query)
        return jsonify({'success': True, 'libraries': results})
    except Exception as e:
        return jsonify({'success': False, 'message': f'搜索RAG库失败: {str(e)}'})

# 知识库管理API

# 文档管理

# 获取文档列表
@app.route('/api/knowledge/documents', methods=['GET'])
def get_knowledge_documents():
    try:
        documents = knowledge_base.get_documents()
        return jsonify({'success': True, 'documents': documents})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取文档列表失败: {str(e)}'})

# 获取文档详情
@app.route('/api/knowledge/documents/<document_id>', methods=['GET'])
def get_knowledge_document(document_id):
    try:
        document = knowledge_base.get_document(document_id)
        if document:
            # 增加浏览量
            knowledge_base.add_view(document_id)
            return jsonify({'success': True, 'document': document})
        else:
            return jsonify({'success': False, 'message': '文档不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取文档详情失败: {str(e)}'})

# 创建文档
@app.route('/api/knowledge/documents', methods=['POST'])
def create_knowledge_document():
    try:
        data = request.get_json()
        document = knowledge_base.create_document(data)
        return jsonify({'success': True, 'document': document})
    except Exception as e:
        return jsonify({'success': False, 'message': f'创建文档失败: {str(e)}'})

# 更新文档
@app.route('/api/knowledge/documents/<document_id>', methods=['PUT'])
def update_knowledge_document(document_id):
    try:
        data = request.get_json()
        document = knowledge_base.update_document(document_id, data)
        if document:
            return jsonify({'success': True, 'document': document})
        else:
            return jsonify({'success': False, 'message': '文档不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'更新文档失败: {str(e)}'})

# 删除文档
@app.route('/api/knowledge/documents/<document_id>', methods=['DELETE'])
def delete_knowledge_document(document_id):
    try:
        success = knowledge_base.delete_document(document_id)
        if success:
            return jsonify({'success': True, 'message': '删除文档成功'})
        else:
            return jsonify({'success': False, 'message': '文档不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'删除文档失败: {str(e)}'})

# 搜索文档
@app.route('/api/knowledge/documents/search', methods=['GET'])
def search_knowledge_documents():
    try:
        query = request.args.get('q', '')
        if not query:
            return jsonify({'success': False, 'message': '搜索关键词不能为空'})
        results = knowledge_base.search_documents(query)
        return jsonify({'success': True, 'documents': results})
    except Exception as e:
        return jsonify({'success': False, 'message': f'搜索文档失败: {str(e)}'})

# 根据分类获取文档
@app.route('/api/knowledge/documents/category/<category_id>', methods=['GET'])
def get_documents_by_category(category_id):
    try:
        documents = knowledge_base.get_documents_by_category(category_id)
        return jsonify({'success': True, 'documents': documents})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取分类文档失败: {str(e)}'})

# 根据标签获取文档
@app.route('/api/knowledge/documents/tag/<tag>', methods=['GET'])
def get_documents_by_tag(tag):
    try:
        documents = knowledge_base.get_documents_by_tag(tag)
        return jsonify({'success': True, 'documents': documents})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取标签文档失败: {str(e)}'})

# 点赞文档
@app.route('/api/knowledge/documents/<document_id>/like', methods=['POST'])
def like_document(document_id):
    try:
        document = knowledge_base.toggle_like(document_id)
        if document:
            return jsonify({'success': True, 'document': document})
        else:
            return jsonify({'success': False, 'message': '文档不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'点赞失败: {str(e)}'})

# 分类管理

# 获取分类列表
@app.route('/api/knowledge/categories', methods=['GET'])
def get_categories():
    try:
        categories = knowledge_base.get_categories()
        return jsonify({'success': True, 'categories': categories})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取分类列表失败: {str(e)}'})

# 获取分类详情
@app.route('/api/knowledge/categories/<category_id>', methods=['GET'])
def get_category(category_id):
    try:
        category = knowledge_base.get_category(category_id)
        if category:
            return jsonify({'success': True, 'category': category})
        else:
            return jsonify({'success': False, 'message': '分类不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取分类详情失败: {str(e)}'})

# 创建分类
@app.route('/api/knowledge/categories', methods=['POST'])
def create_category():
    try:
        data = request.get_json()
        category = knowledge_base.create_category(data)
        return jsonify({'success': True, 'category': category})
    except Exception as e:
        return jsonify({'success': False, 'message': f'创建分类失败: {str(e)}'})

# 更新分类
@app.route('/api/knowledge/categories/<category_id>', methods=['PUT'])
def update_category(category_id):
    try:
        data = request.get_json()
        category = knowledge_base.update_category(category_id, data)
        if category:
            return jsonify({'success': True, 'category': category})
        else:
            return jsonify({'success': False, 'message': '分类不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'更新分类失败: {str(e)}'})

# 删除分类
@app.route('/api/knowledge/categories/<category_id>', methods=['DELETE'])
def delete_category(category_id):
    try:
        success = knowledge_base.delete_category(category_id)
        if success:
            return jsonify({'success': True, 'message': '删除分类成功'})
        else:
            return jsonify({'success': False, 'message': '分类不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'删除分类失败: {str(e)}'})

# 标签管理

# 获取标签列表
@app.route('/api/knowledge/tags', methods=['GET'])
def get_tags():
    try:
        tags = knowledge_base.get_tags()
        return jsonify({'success': True, 'tags': tags})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取标签列表失败: {str(e)}'})

# 获取标签详情
@app.route('/api/knowledge/tags/<tag_id>', methods=['GET'])
def get_tag(tag_id):
    try:
        tag = knowledge_base.get_tag(tag_id)
        if tag:
            return jsonify({'success': True, 'tag': tag})
        else:
            return jsonify({'success': False, 'message': '标签不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取标签详情失败: {str(e)}'})

# 创建标签
@app.route('/api/knowledge/tags', methods=['POST'])
def create_tag():
    try:
        data = request.get_json()
        tag = knowledge_base.create_tag(data)
        return jsonify({'success': True, 'tag': tag})
    except Exception as e:
        return jsonify({'success': False, 'message': f'创建标签失败: {str(e)}'})

# 更新标签
@app.route('/api/knowledge/tags/<tag_id>', methods=['PUT'])
def update_tag(tag_id):
    try:
        data = request.get_json()
        tag = knowledge_base.update_tag(tag_id, data)
        if tag:
            return jsonify({'success': True, 'tag': tag})
        else:
            return jsonify({'success': False, 'message': '标签不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'更新标签失败: {str(e)}'})

# 删除标签
@app.route('/api/knowledge/tags/<tag_id>', methods=['DELETE'])
def delete_tag(tag_id):
    try:
        success = knowledge_base.delete_tag(tag_id)
        if success:
            return jsonify({'success': True, 'message': '删除标签成功'})
        else:
            return jsonify({'success': False, 'message': '标签不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'删除标签失败: {str(e)}'})

# 知识库扩展API

# 获取文档版本列表
@app.route('/api/knowledge/documents/<document_id>/versions', methods=['GET'])
def get_document_versions(document_id):
    try:
        versions = knowledge_base.get_document_versions(document_id)
        return jsonify({'success': True, 'versions': versions})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取文档版本列表失败: {str(e)}'})

# 获取指定文档版本
@app.route('/api/knowledge/documents/versions/<version_id>', methods=['GET'])
def get_document_version(version_id):
    try:
        version = knowledge_base.get_document_version(version_id)
        if version:
            return jsonify({'success': True, 'version': version})
        else:
            return jsonify({'success': False, 'message': '版本不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取文档版本失败: {str(e)}'})

# 恢复文档到指定版本
@app.route('/api/knowledge/documents/<document_id>/versions/<version_id>/restore', methods=['POST'])
def restore_document_version(document_id, version_id):
    try:
        document = knowledge_base.restore_document_version(document_id, version_id)
        if document:
            return jsonify({'success': True, 'document': document})
        else:
            return jsonify({'success': False, 'message': '文档或版本不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'恢复文档版本失败: {str(e)}'})

# 添加文档权限
@app.route('/api/knowledge/documents/<document_id>/permissions', methods=['POST'])
def add_document_permission(document_id):
    try:
        data = request.get_json()
        # 添加document_id到数据中
        data['document_id'] = document_id
        permission = knowledge_base.add_document_permission(data)
        return jsonify({'success': True, 'permission': permission})
    except Exception as e:
        return jsonify({'success': False, 'message': f'添加文档权限失败: {str(e)}'})

# 获取文档权限
@app.route('/api/knowledge/documents/<document_id>/permissions', methods=['GET'])
def get_document_permissions(document_id):
    try:
        permissions = knowledge_base.get_document_permissions(document_id)
        return jsonify({'success': True, 'permissions': permissions})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取文档权限失败: {str(e)}'})

# 更新文档权限
@app.route('/api/knowledge/documents/permissions/<permission_id>', methods=['PUT'])
def update_document_permission(permission_id):
    try:
        data = request.get_json()
        permission = knowledge_base.update_document_permission(permission_id, data)
        if permission:
            return jsonify({'success': True, 'permission': permission})
        else:
            return jsonify({'success': False, 'message': '权限不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'更新文档权限失败: {str(e)}'})

# 删除文档权限
@app.route('/api/knowledge/documents/permissions/<permission_id>', methods=['DELETE'])
def delete_document_permission(permission_id):
    try:
        success = knowledge_base.delete_document_permission(permission_id)
        if success:
            return jsonify({'success': True, 'message': '删除文档权限成功'})
        else:
            return jsonify({'success': False, 'message': '权限不存在'})
    except Exception as e:
        return jsonify({'success': False, 'message': f'删除文档权限失败: {str(e)}'})

# 检查文档权限
@app.route('/api/knowledge/documents/<document_id>/check-permission', methods=['GET'])
def check_document_permission(document_id):
    try:
        user_id = request.args.get('user_id', '')
        permission_type = request.args.get('permission_type', 'read')
        has_permission = knowledge_base.check_document_permission(document_id, user_id, permission_type)
        return jsonify({'success': True, 'has_permission': has_permission})
    except Exception as e:
        return jsonify({'success': False, 'message': f'检查文档权限失败: {str(e)}'})

# 获取知识库统计信息
@app.route('/api/knowledge/statistics', methods=['GET'])
def get_knowledge_statistics():
    try:
        statistics = knowledge_base.get_document_statistics()
        return jsonify({'success': True, 'statistics': statistics})
    except Exception as e:
        return jsonify({'success': False, 'message': f'获取知识库统计信息失败: {str(e)}'})

import socket
import subprocess
import sys

def is_port_in_use(port):
    """检查端口是否被占用"""
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.settimeout(1)
        result = s.connect_ex(('127.0.0.1', port))
        return result == 0

def get_process_using_port(port):
    """获取占用端口的进程信息"""
    try:
        # 使用netstat命令获取端口占用情况
        cmd = f'netstat -ano | findstr :{port}'
        result = subprocess.check_output(cmd, shell=True, text=True)
        # 提取PID
        lines = result.strip().split('\n')
        if lines:
            # 取最后一行的PID
            last_line = lines[-1]
            pid = last_line.split()[-1]
            # 获取进程名称
            cmd = f'tasklist /fi "PID eq {pid}" /fo csv /nh'
            task_result = subprocess.check_output(cmd, shell=True, text=True)
            return pid, task_result.strip()
    except Exception as e:
        pass
    return None, None

def is_hos_me(pid):
    """判断进程是否为HOS ME办公平台"""
    try:
        # 获取进程命令行
        cmd = f'wmic process where ProcessID={pid} get CommandLine /format:list'
        result = subprocess.check_output(cmd, shell=True, text=True)
        # 检查命令行是否包含HOS ME办公平台相关信息
        return 'weekly_report_tool.py' in result or 'app.py' in result or 'HOS_ME' in result
    except Exception as e:
        pass
    return False

def get_local_ip():
    """获取本地IP地址"""
    try:
        # 创建一个UDP socket，不需要连接
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        # 连接到一个公共的IP地址，这里使用Google的DNS服务器
        s.connect(('8.8.8.8', 80))
        # 获取本地IP地址
        local_ip = s.getsockname()[0]
        s.close()
        return local_ip
    except Exception:
        return '127.0.0.1'

def main():
    """主函数，处理端口自动分配"""
    base_port = 50000
    max_attempts = 100
    use_port = base_port
    local_ip = get_local_ip()
    
    # ASCII ME logo
    me_logo = """
  __  __  ____  ____  _  _  ____  __  __  _  _ 
 (  \/  )(  _ \(  _ \( \/ )( ___)(  \/  )( \( )
  )    (  ) _ < ) __/ \  /  )__)  )    (  )  ( 
 (_/\/\_)(____/(__)   \/  (____)(_/\/\_)(_)\_)
    """
    
    print(me_logo)
    print("                    manager ender")
    print(f"正在启动，基础端口: {base_port}")
    
    for attempt in range(max_attempts):
        print(f"检测端口: {use_port}")
        
        if not is_port_in_use(use_port):
            # 端口可用，直接使用
            print(f"端口 {use_port} 可用，使用该端口启动服务")
            break
        
        # 端口被占用，检查是否是HOS办公平台
        pid, process_info = get_process_using_port(use_port)
        if pid:
            print(f"端口 {use_port} 已被占用，PID: {pid}")
            print(f"进程信息: {process_info}")
            
            # 检查是否为HOS ME办公平台
            if is_hos_me(pid):
                print("检测到这是另一个HOS办公平台实例")
                
                # 询问用户选择
                print("\n请选择操作：")
                print("1. 尝试使用下一个可用端口")
                print("2. 关闭现有HOS办公平台并在当前端口启动")
                print("3. 退出程序")
                
                choice = input("请输入选择 (1/2/3): ").strip()
                
                if choice == '1':
                    print("将尝试使用下一个端口")
                elif choice == '2':
                    # 关闭现有HOS办公平台进程
                    print(f"正在关闭占用端口 {use_port} 的HOS办公平台进程...")
                    try:
                        subprocess.run(f'taskkill /pid {pid} /f', shell=True, check=True)
                        print(f"进程 {pid} 已成功关闭")
                        print(f"现在使用端口 {use_port} 启动服务")
                        break
                    except subprocess.CalledProcessError:
                        print(f"关闭进程 {pid} 失败，将尝试使用下一个端口")
                elif choice == '3':
                    print("程序退出")
                    sys.exit(0)
                else:
                    print("无效选择，将尝试使用下一个端口")
            else:
                print("这不是HOS办公平台实例，继续尝试下一个端口")
        
        # 尝试下一个端口
        use_port += 1
    else:
        print(f"尝试了 {max_attempts} 个端口，均被占用，程序退出")
        sys.exit(1)
    
    print(f"\n启动HOS办公平台服务...")
    print(f"本地访问地址: http://127.0.0.1:{use_port}")
    print(f"网络访问地址: http://{local_ip}:{use_port}")
    print("按 Ctrl+C 停止服务\n")
    
    # 启动Flask应用
    app.run(host='127.0.0.1', port=use_port, debug=False, threaded=True)

if __name__ == '__main__':
    main()
