import os
import time
import datetime
import pandas as pd
from typing import List
from common import inference_model
import logging
import numpy as np
import yaml
from feature_process import create_feature_df, apply_feature_weights, Features, process_features_list

# 模拟发送的特征数据
features_data1 = {
    "somatization": 0.5,
    "obsessive_compulsive": 0.3,
    "interpersonal_sensitivity": 0.7,
    "depression": 0.6,
    "anxiety": 0.8,
    "hostility": 0.4,
    "terror": 0.2,
    "paranoia": 0.1,
    "psychoticism": 0.9,
    "other": 0.2,
    "father_parenting_style": 2,
    "mother_parenting_style": 3,
    "self_assessed_family_economic_condition": 4,
    "history_of_psychological_counseling": 1,
    "absenteeism_above_average": True,
    "academic_warning": False,
    "label": -1
}
features_data2 = {
    "somatization": 4.3,
    "obsessive_compulsive": 4.1,
    "interpersonal_sensitivity": 3.8,
    "depression": 4,
    "anxiety": 4.2,
    "hostility": 4.2,
    "terror": 4,
    "paranoia": 4.2,
    "psychoticism": 3.8,
    "other": 3.6,
    "father_parenting_style": 1,
    "mother_parenting_style": 0,
    "self_assessed_family_economic_condition": 0,
    "history_of_psychological_counseling": False,
    "absenteeism_above_average": True,
    "academic_warning": True,
    "label": -1
}
features_data3 = {
    "somatization": 1.1,
    "obsessive_compulsive": 2.3,
    "interpersonal_sensitivity": 2.6,
    "depression": 2.2,
    "anxiety": 1.6,
    "hostility": 1.5,
    "terror": 1.6,
    "paranoia": 1.5,
    "psychoticism": 1.2,
    "other": 1.3,
    "father_parenting_style": 1,
    "mother_parenting_style": 1,
    "self_assessed_family_economic_condition": 1,
    "history_of_psychological_counseling": False,
    "absenteeism_above_average": False,
    "academic_warning": False,
    "label": -1
}
features_data4 = {
    "somatization": 1.5,
    "obsessive_compulsive": 2.4,
    "interpersonal_sensitivity": 2.2,
    "depression": 3.2,
    "anxiety": 1.6,
    "hostility": 1.5,
    "terror": 3.1,
    "paranoia": 1.5,
    "psychoticism": 1.9,
    "other": 2.6,
    "father_parenting_style": 1,
    "mother_parenting_style": 1,
    "self_assessed_family_economic_condition": 0,
    "history_of_psychological_counseling": False,
    "absenteeism_above_average": False,
    "academic_warning": False,
    "label": -1
}

# 必须是features_list
# features_data_list = [features_data3]
features_data_list = [features_data4, features_data2, features_data3, features_data1]

if __name__ == "__main__":

    processed_features_list: List[Features] = process_features_list(features_data_list)
    
    # 特征预处理
    all_features = create_feature_df(processed_features_list)

    # 读取 YAML 配置文件
    config_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "train_local.yaml"))
    with open(config_path, 'r') as f:
        config = yaml.load(f, Loader=yaml.FullLoader)
        feature_names = config['feature_names']
        feature_weights = config['feature_weights']

    # 应用特征权重
    feature_label_weighted = apply_feature_weights(all_features, feature_names, feature_weights)

    start_time = time.time()  # 记录开始时间

    # 创建静态文件存放文件夹
    static_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "evaluate_local"))  # 设置模型文件和配置文件的存放目录，和本py同级
    os.makedirs(static_dir, exist_ok=True)

    # 训练前设置
    now = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
    data_path = os.path.abspath(os.path.join(os.path.dirname(__file__), static_dir, f"all_features_label_{now}.xlsx"))
    config['data_path'] = data_path
    feature_label_weighted.to_excel(data_path, index=False)

    # 配置日志
    log_path = os.path.abspath(os.path.join(os.path.dirname(__file__), static_dir, f"evaluate_{now}.log"))
    logging.basicConfig(filename=log_path, level=logging.INFO, format='%(asctime)s %(levelname)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
    
    # 开始验证
    list_avg_f1 = []
    list_wrong_percentage = []
    list_precision = []
    list_recall = []
    list_f1 = []

    # 特征和标签
    X = feature_label_weighted[config['feature_names']].values
    y = feature_label_weighted[config['label_name']].values

    print(config)
    predictions = inference_model(config["model_path"], X, y, config)
    end_time = time.time()  # 记录结束时间
    print("预测耗时:", end_time - start_time, "秒")  # 打印执行时间

    print("预测结果:", predictions)