import threading
import time
from queue import Empty, Full

from flask import Blueprint, jsonify, request

from config import JSON_TASK_QUEUE,runing_task


communication_bp = Blueprint('communication', __name__)

_del_brand_cache = {}
_del_brand_cache_lock = threading.Lock()
_latest_del_brand_item = None
_latest_del_brand_ts = 0.0


def _resolve_request_id():
    rid = request.args.get('id')
    if rid is not None and str(rid).strip() != '':
        return str(rid)
    if request.method == 'POST':
        body = request.get_json(silent=True) or {}
        rid = body.get('id')
        if rid is not None and str(rid).strip() != '':
            return str(rid)
    return None


@communication_bp.route('/api/amazon/del_brand', methods=['GET', 'POST'])
def del_brand():
    """返回队列中的一个元素；同一 id 重复请求返回已缓存的同一元素；2分钟内不同 id 也返回最近元素。"""
    global _latest_del_brand_item, _latest_del_brand_ts
    request_id = _resolve_request_id()
    if not request_id:
        return jsonify({'success': False, 'message': '缺少参数 id', 'data': None}), 400

    with _del_brand_cache_lock:
        if request_id in _del_brand_cache:
            item = _del_brand_cache[request_id]
            print("【删除品牌】缓存命中", request_id, item)
            return jsonify({'success': True, 'data': item})
        if _latest_del_brand_item is not None and (time.time() - _latest_del_brand_ts) <= 120:
            _del_brand_cache[request_id] = _latest_del_brand_item
            print("【删除品牌】2分钟窗口命中", request_id, _latest_del_brand_item)
            return jsonify({'success': True, 'data': _latest_del_brand_item})

    try:
        item = JSON_TASK_QUEUE.get_nowait()
    except Empty:
        print("队列为空")
        return jsonify({'success': False, 'message': '队列为空', 'data': None})

    with _del_brand_cache_lock:
        if request_id in _del_brand_cache:
            try:
                JSON_TASK_QUEUE.put_nowait(item)
            except Full:
                print("【删除品牌】队列已满，无法归还重复拉取的任务")
            item = _del_brand_cache[request_id]
            print("【删除品牌】并发归并", request_id, item)
            return jsonify({'success': True, 'data': item})
        _del_brand_cache[request_id] = item
        _latest_del_brand_item = item
        _latest_del_brand_ts = time.time()
        print("【删除品牌】返回任务数据", item)
        return jsonify({'success': True, 'data': item})
    

@communication_bp.route('/api/amazon/get_all_detail', methods=['GET', 'POST'])
def get_all_detail():
    """获取正在执行的删除品牌任务详情"""
    request_id = _resolve_request_id()
    if not request_id:  #如果没有提供 id 参数，直接返回所有正在执行的任务详情
        return jsonify({'success': True, 'data': runing_task})
    task_info = runing_task.get(request_id)
    if not task_info:
        return jsonify({'success': False, 'message': '任务不存在', 'data': None})
    return jsonify({'success': True, 'data': task_info})



