"""
日志管理 API 路由
"""
import os
import sys
import logging
import yaml
from datetime import datetime
from logging.handlers import TimedRotatingFileHandler, RotatingFileHandler
from flask import Blueprint, request, jsonify

# 获取项目根目录
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.insert(0, PROJECT_ROOT)

# 加载配置
CONFIG_PATH = os.path.join(PROJECT_ROOT, 'config.yaml')
with open(CONFIG_PATH, 'r', encoding='utf-8') as f:
    CONFIG = yaml.safe_load(f)

# 日志目录
LOG_BASE_DIR = os.path.join(PROJECT_ROOT, 'logs')

# 创建蓝图
logs_bp = Blueprint('logs', __name__, url_prefix='/api')


def setup_logs_logger():
    """配置日志系统，按日期分目录存储"""
    today = datetime.now().strftime('%Y-%m-%d')
    log_dir = os.path.join(LOG_BASE_DIR, today)
    os.makedirs(log_dir, exist_ok=True)

    logger = logging.getLogger('logs_api')
    logger.setLevel(logging.DEBUG)
    logger.handlers.clear()

    # 全部日志处理器
    all_log_path = os.path.join(log_dir, 'all.log')
    all_handler = TimedRotatingFileHandler(all_log_path, when='midnight', interval=1, backupCount=30, encoding='utf-8')
    all_handler.setLevel(logging.DEBUG)
    all_handler.setFormatter(logging.Formatter(
        '[%(asctime)s] %(levelname)s in %(module)s: %(message)s',
        datefmt='%Y-%m-%d %H:%M:%S'
    ))

    # 控制台处理器
    console_handler = logging.StreamHandler()
    console_handler.setLevel(logging.INFO)
    console_handler.setFormatter(logging.Formatter(
        '[%(asctime)s] %(levelname)s: %(message)s',
        datefmt='%H:%M:%S'
    ))

    logger.addHandler(all_handler)
    logger.addHandler(console_handler)

    return logger


logs_logger = setup_logs_logger()


@logs_bp.route('/web-log', methods=['POST'])
def receive_web_log():
    """接收前端页面发送的日志"""
    data = request.json
    level = data.get('level', 'info')
    message = data.get('message', '')
    page = data.get('page', 'unknown')
    timestamp = data.get('timestamp', datetime.now().isoformat())

    log_message = f'[WEB-{page}] {message}'

    if level == 'error':
        logs_logger.error(log_message)
    elif level == 'warning':
        logs_logger.warning(log_message)
    elif level == 'debug':
        logs_logger.debug(log_message)
    else:
        logs_logger.info(log_message)

    return jsonify({'code': 0, 'message': '日志接收成功'})


def format_file_size(size_bytes):
    """格式化文件大小"""
    if size_bytes < 1024:
        return f'{size_bytes} B'
    elif size_bytes < 1024 * 1024:
        return f'{size_bytes / 1024:.1f} KB'
    else:
        return f'{size_bytes / (1024 * 1024):.1f} MB'


@logs_bp.route('/log-files', methods=['GET'])
def get_log_files():
    """获取指定日期的日志文件列表"""
    date = request.args.get('date')
    if not date:
        return jsonify({'code': 1, 'message': '缺少日期参数'})

    # 验证日期格式
    try:
        datetime.strptime(date, '%Y-%m-%d')
    except ValueError:
        return jsonify({'code': 1, 'message': '日期格式错误，应为 YYYY-MM-DD'})

    log_dir = os.path.join(LOG_BASE_DIR, date)

    if not os.path.exists(log_dir):
        return jsonify({'code': 0, 'data': []})

    log_files = []
    file_names = ['all.log', 'error.log', 'request.log']

    for file_name in file_names:
        file_path = os.path.join(log_dir, file_name)
        if os.path.exists(file_path):
            size = os.path.getsize(file_path)
            log_files.append({
                'name': file_name.replace('.log', ''),
                'file': f'{date}/{file_name}',
                'size': format_file_size(size)
            })

    return jsonify({'code': 0, 'data': log_files})


@logs_bp.route('/log-content', methods=['GET'])
def get_log_content():
    """获取日志文件内容"""
    file_path = request.args.get('file')
    if not file_path:
        return jsonify({'code': 1, 'message': '缺少文件参数'})

    # 防止目录遍历攻击
    file_path = file_path.replace('..', '').replace('//', '/')
    full_path = os.path.join(LOG_BASE_DIR, file_path)

    # 验证文件路径是否在日志目录下
    if not full_path.startswith(LOG_BASE_DIR):
        return jsonify({'code': 1, 'message': '无效的文件路径'})

    if not os.path.exists(full_path) or not os.path.isfile(full_path):
        return jsonify({'code': 1, 'message': '日志文件不存在'})

    try:
        size = os.path.getsize(full_path)
        # 限制读取大小，最大 5MB
        max_size = 5 * 1024 * 1024
        if size > max_size:
            with open(full_path, 'r', encoding='utf-8') as f:
                f.seek(size - max_size)
                content = '... (日志文件较大，已显示最后 5MB 内容) ...\n\n' + f.read()
        else:
            with open(full_path, 'r', encoding='utf-8') as f:
                content = f.read()

        return jsonify({
            'code': 0,
            'data': {
                'file': os.path.basename(file_path),
                'path': file_path,
                'size': format_file_size(size),
                'content': content
            }
        })
    except Exception as e:
        return jsonify({'code': 1, 'message': f'读取日志文件失败: {str(e)}'})
