import openpyxl
import json
import os
from typing import List, Dict, Any, Tuple

from Config.Config import EXCEL_PATH
from Util.DataUtil import (
    init_directories, process_value, print_conversion_stats,
    convert_area_name, save_to_json, load_workbook_sheet
)

# ======================= 配置常量 =======================
DATA_DIR = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'Data')
JSON_PATH = os.path.join(DATA_DIR, 'MaoRuXueLv.json')
SHEET_NAME = '毛入学率'
START_ROW = 5
REGION_NAME_COLUMN = 'B'
YEAR_RANGE = [2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024]

# 数据列映射配置
DATA_COLUMNS = {
    # 学前教育 - 交替列映射
    'preschool_enrollment': {
        'columns': ['D', 'F', 'H', 'J', 'L', 'N', 'P', 'R', 'T', 'V'],
        'years': YEAR_RANGE
    },
    'preschool_enrollment_rate': {
        'columns': ['E', 'G', 'I', 'K', 'M', 'O', 'Q', 'S', 'U', 'W'],
        'years': YEAR_RANGE
    },
    
    # 小学教育
    'primary_enrollment': {
        'columns': ['X', 'Z', 'AB', 'AD', 'AF', 'AH', 'AJ', 'AL', 'AN', 'AP'],
        'years': YEAR_RANGE
    },
    'primary_enrollment_rate': {
        'columns': ['Y', 'AA', 'AC', 'AE', 'AG', 'AI', 'AK', 'AM', 'AO', 'AQ'],
        'years': YEAR_RANGE
    },
    
    # 初中教育
    'junior_high_enrollment': {
        'columns': ['AR', 'AT', 'AV', 'AX', 'AZ', 'BB', 'BD', 'BF', 'BH', 'BJ'],
        'years': YEAR_RANGE
    },
    'junior_high_enrollment_rate': {
        'columns': ['AS', 'AU', 'AW', 'AY', 'BA', 'BC', 'BE', 'BG', 'BI', 'BK'],
        'years': YEAR_RANGE
    },
    
    # 普通高中教育
    'senior_high_enrollment': {
        'columns': ['BL', 'BN', 'BP', 'BR', 'BT', 'BV', 'BX', 'BZ', 'CB', 'CD'],
        'years': YEAR_RANGE
    },
    'senior_high_enrollment_rate': {
        'columns': ['BM', 'BO', 'BQ', 'BS', 'BU', 'BW', 'BY', 'CA', 'CC', 'CE'],
        'years': YEAR_RANGE
    },
    
    # 中职教育
    'vocational_enrollment': {
        'columns': ['CF', 'CH', 'CJ', 'CL', 'CN', 'CP', 'CR', 'CT', 'CV', 'CX'],
        'years': YEAR_RANGE
    },
    'vocational_enrollment_rate': {
        'columns': ['CG', 'CI', 'CK', 'CM', 'CO', 'CQ', 'CS', 'CU', 'CW', 'CY'],
        'years': YEAR_RANGE
    }
}

# ======================= 核心逻辑 =======================
def extract_enrollment_data(sheet: openpyxl.worksheet.worksheet.Worksheet) -> Tuple[List[Dict[str, Any]], List[Dict[str, str]], List[str]]:
    """从工作表提取毛入学率数据"""
    enrollment_data: List[Dict[str, Any]] = []
    conversion_records: List[Dict[str, str]] = []
    name_conversion_errors: List[str] = []
    
    # 计算区域名称列索引
    region_col_idx = openpyxl.utils.column_index_from_string(REGION_NAME_COLUMN) - 1
    
    # 遍历数据行
    for row_num, row in enumerate(sheet.iter_rows(min_row=START_ROW, values_only=True), start=START_ROW):
        # 获取区域名称
        raw_name = row[region_col_idx] if (len(row) > region_col_idx and row[region_col_idx] is not None) else '未知地区'
        
        # 转换区域名称
        area_name, area_code, conv_records, errors = convert_area_name(raw_name, row_num)
        conversion_records.extend(conv_records)
        name_conversion_errors.extend(errors)
        
        if not area_name:
            continue
        
        # 创建区域数据对象
        area_data = {
            'area_name': area_name,
            'area_code': area_code,
            'raw_name': str(raw_name).strip() if raw_name else '未知地区'
        }
        
        # 提取各指标年度数据
        for metric, config in DATA_COLUMNS.items():
            year_data = {}
            if 'columns' in config and 'years' in config:
                for col_name, year in zip(config['columns'], config['years']):
                    col_idx = openpyxl.utils.column_index_from_string(col_name) - 1
                    if col_idx < len(row):
                        value = row[col_idx]
                        year_data[str(year)] = process_value(value)
            area_data[metric] = year_data
        
        enrollment_data.append(area_data)
    
    return enrollment_data, conversion_records, name_conversion_errors

# ======================= 主函数 =======================
def main() -> None:
    """主函数：执行毛入学率数据提取流程"""
    init_directories(DATA_DIR)
    
    # 加载工作表
    sheet = load_workbook_sheet(EXCEL_PATH, SHEET_NAME)
    if not sheet:
        return
    
    # 提取数据
    enrollment_data, conversion_records, name_conversion_errors = extract_enrollment_data(sheet)
    
    # 保存结果
    save_to_json(enrollment_data, JSON_PATH)
    
    # 输出转换统计
    print_conversion_stats(conversion_records, name_conversion_errors)

if __name__ == '__main__':
    main()

