#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import warnings
import time
from multiprocessing import Pool
from pathlib import Path

root_path = Path(__file__).resolve().parent.parent
sys.path.append(str(root_path))
from config import config
from models.common import log  # 确保这个路径是正确的，或者调整它
from single_case_eval import single_case_eval  # 确保这个路径是正确的，或者调整它

root_path = Path(__file__).resolve().parent.parent
sys.path.append(str(root_path))

log_path = config.LOG_PATH
print(log_path)
logger = log.get_logger(log_path)


def main():
    # 忽略所有警告
    warnings.filterwarnings("ignore")
    data_path = config.PROCESSED_DATA_PATH
    if not data_path:
        logger.error("Config file missing 'processed_data_path' key.")
        return

    logger.info(f"[case:{data_path}] SINGLE_CASE_EVAL: Start.")

    t1 = time.time()
    subfolders = [f.name for f in os.scandir(data_path) if f.is_dir()]
    count = len(subfolders)
    logger.info(f"{count} subfolders found to process: {subfolders}")
    if not subfolders:
        logger.warning("No subfolders found to process.")
        return

    mode_label = "PGVIL"  # 根据需要替换

    # single_case_eval(subfolders[0], mode_label)
    # 创建一个进程池
    with Pool(processes=min(count, os.cpu_count())) as pool:
        # 准备参数
        params = [(case_name, mode_label) for case_name in subfolders]
        try:
            # 使用进程池执行函数，这里的results没有用，是因为后边要组合成多用例报告评估用，现在还没想好，
            # 如果想要多用例报告评估，可以在此处修改
            results = pool.starmap(single_case_eval, params)
        except Exception as e:
            logger.error(f"An error occurred during processing: {e}", exc_info=True)
            return

    logger.info("所有子文件夹处理完成")
    t2 = time.time()
    logger.info(f"程序结束，执行时间：{int(t2 - t1)} s")
    logger.info(f"[case:{data_path}] SINGLE_CASE_EVAL: End.")


if __name__ == "__main__":
    main()
