from flask import Blueprint, jsonify, request
from config import config_manager
from utils.plex_utils import PlexManager
from plexapi.exceptions import Unauthorized, NotFound
from services.cache_service import CacheManager
from utils.pagination import paginate
media_bp = Blueprint('media', __name__, url_prefix='/api')

@media_bp.route('/ptskit/torrents', methods=['GET'])
def get_ptskit_torrents():
    # 逻辑: 访问 PTSKIT 网站，解析种子列表页，支持分页和搜索
    # 返回格式: {"total": int, "list": [{"id": str, "title": str, ...}]}
    page = request.args.get('page', 1, type=int)
    page_size = request.args.get('pageSize', 20, type=int)
    keyword = request.args.get('keyword', '')
    
    mock_list = [{
        "id": f"torrent-{i + (page-1)*page_size}",
        "title": f"[PTSKIT] 搜索'{keyword}'的结果 {i+1}",
        "size": f"{i+1}.5 GB",
        "seeders": 50 - i, "leechers": i,
        "download_url": f"http://example.com/download.php?id={i}"
    } for i in range(page_size)]
    
    return jsonify({"total": 123, "list": mock_list})

@media_bp.route('/local-media', methods=['GET'])
def get_local_media():
    # 逻辑: 扫描配置中的 skit_paths 目录，列出所有短剧文件夹
    # 返回格式: {"total": int, "list": [{"id": str, "title": str, "path": str, ...}]}
    page = request.args.get('page', 1, type=int)
    page_size = request.args.get('pageSize', 20, type=int)
    
    mock_list = [{
        "id": f"local-{i + (page-1)*page_size}",
        "title": f"本地短剧-{i+1}",
        "path": f"/skits/本地短剧-{i+1}",
        "poster": f"https://picsum.photos/seed/{i}/200/300",
        "scraped": i % 2 == 0
    } for i in range(page_size)]

    return jsonify({"total": 88, "list": mock_list})

@media_bp.route('/<server_type>/library', methods=['GET'])
def get_media_server_library(server_type):
    # 逻辑: 根据 server_type (emby, plex, fnos) 连接对应服务，获取媒体库
    # 返回格式: {"total": int, "list": [{"id": str, "title": str, ...}]}
    # if server_type not in ['emby', 'plex', 'fnos']:
        # return jsonify({"error": "不支持的媒体服务器类型"}), 404
    if server_type == 'plex':
        try:
            plex_config = config_manager.get('plex')
            manager = PlexManager(plex_config)
            libraries = manager.get_libraries()
            return jsonify({"total": len(libraries), "list": libraries})
        except ValueError as e: # 通常是未配置错误
            return jsonify({"error": str(e)}), 400
        except Unauthorized as e:
            return jsonify({"error": f"Plex 认证失败: {e}"}), 401
        except Exception as e:
            return jsonify({"error": f"连接 Plex 时发生错误: {e}"}), 503
    elif server_type in ['emby', 'fnos']:
        page = request.args.get('page', 1, type=int)
        page_size = request.args.get('pageSize', 20, type=int)

        mock_list = [{
            "id": f"{server_type}-{i + (page-1)*page_size}",
            "title": f"[{server_type.upper()}] 服务器上的短剧 {i+1}",
            "year": 2023,
            "poster": f"https://picsum.photos/seed/{server_type}{i}/200/300"
        } for i in range(page_size)]

        return jsonify({"total": 66, "list": mock_list})
    return jsonify({"error": "不支持的媒体服务器类型"}), 404


@media_bp.route('/plex/libraries/<library_id>/items', methods=['GET'])
def get_plex_library_items(library_id):
    page = request.args.get('page', 1, type=int)
    page_size = request.args.get('pageSize', 20, type=int)
    try:
        # 1. 初始化缓存管理器，为每个库使用单独的缓存文件
        # TTL 设置为 3660 秒（1小时+1分钟），确保比定时任务间隔稍长
        cache_mgr = CacheManager(f"plex_lib_{library_id}.json", ttl_seconds=3660)
        
        # 2. 初始化 Plex 管理器
        plex_config = config_manager.get('plex')
        manager = PlexManager(plex_config)
        # 3. 使用 cache_mgr.get_data 获取数据
        # 它会自动处理：读缓存，或在缓存失效时调用 manager.get_library_items
        all_items = cache_mgr.get_data(manager.get_library_items, library_id=library_id)
        # 4. 使用分页工具进行分页
        paginated_data = paginate(all_items, page, page_size)
        return jsonify(paginated_data)
    except ValueError as e: # 通常是配置错误
        return jsonify({"error": str(e)}), 400
    except NotFound: # get_library_items 可能会在强制刷新时抛出
        return jsonify({"error": f"ID为 '{library_id}' 的 Plex 媒体库不存在"}), 4404
    except Unauthorized as e:
        return jsonify({"error": f"Plex 认证失败: {e}"}), 401
    except Exception as e:
        return jsonify({"error": f"获取 Plex 项目时发生内部错误: {e}"}), 500