import os
import sys

sys.path.append(os.path.dirname(sys.path[0]))
from utils.db_util import DBUtil
from utils.ssh_util import SSHUtil
from utils.common_util import CommonUtil, DateTypes
from utils.hdfs_utils import HdfsUtils

if __name__ == '__main__':
    site_name = CommonUtil.get_sys_arg(1, None)
    date_type = CommonUtil.get_sys_arg(2, None)
    date_info = CommonUtil.get_sys_arg(3, None)
    test_flag = CommonUtil.get_sys_arg(len(sys.argv) - 1, None)
    print(f"执行参数为{sys.argv}")

    assert site_name == 'us', "本表仅 us 站点导出"
    assert date_type == DateTypes.month.name, "本脚本仅支持 month 类型（基准月口径）"

    if test_flag == 'test':
        db_type = 'postgresql_test'
        print("导出到测试库中")
    else:
        # 工时校验（与新 ABA 流程其他导出脚本一致）
        CommonUtil.judge_is_work_hours(
            site_name=site_name, date_type=date_type, date_info=date_info,
            principal='hejiangming',
            priority=2,
            export_tools_type=1,
            belonging_to_process=f'新ABA流程年词频热度_{date_type}'
        )
        db_type = "postgresql_cluster"
        print("导出到PG集群中")

    # 1) 校验 Hive 分区有数据
    hive_partition_path = (
        f"/home/big_data_selection/dws/dws_aba_word_heat_last365/"
        f"site_name={site_name}/date_type={date_type}/date_info={date_info}"
    )
    hive_files = HdfsUtils.read_list(hive_partition_path)
    if not hive_files:
        print(f"[ERROR] Hive 分区无数据文件，路径：{hive_partition_path}，跳过导出！")
        sys.exit(1)
    print(f"Hive 分区文件数：{len(hive_files)}，路径：{hive_partition_path}，继续导出")

    # 2) 表名拼装（年表固定，不带年份后缀）
    export_tb_target = "us_aba_word_heat_last_365_day"   # 正式表
    export_tb_copy = f"{export_tb_target}_copy"          # copy 表

    engine = DBUtil.get_db_engine(db_type, site_name)

    # 3) 创建 copy 表（drop + create like including comments），并 ALTER 数组列为 VARCHAR
    # 用 `including comments` 而不是 `including all`：
    #   only 复制列定义和注释，不复制索引/约束，避免主键冲突等问题（与 dwt_aba_last365 同款）
    with engine.connect() as connection:
        sql = f"""
            drop table if exists {export_tb_copy};
            create table if not exists {export_tb_copy}
            (
                like {export_tb_target} including comments
            );
            ALTER TABLE {export_tb_copy} ALTER COLUMN theme_ch_list TYPE VARCHAR(200);
        """
        print("================================执行 SQL================================")
        print(sql)
        connection.execute(sql)

    # 4) Sqoop 导出（字段顺序与 Hive 表 schema 一致，分区字段 date_info 放最后）
    sh = CommonUtil.build_export_sh(
        site_name=site_name,
        db_type=db_type,
        hive_tb="dws_aba_word_heat_last365",
        export_tb=export_tb_copy,
        col=[
            "word",
            "word_heat",
            "word_heat_change_rate",
            "theme_ch_list",
            "created_time",
            "updated_time",
            "date_info"
        ],
        partition_dict={
            "site_name": site_name,
            "date_type": date_type,
            "date_info": date_info
        }
    )

    client = SSHUtil.get_ssh_client()
    SSHUtil.exec_command_async(client, sh, ignore_err=False)
    client.close()

    # 5) 交换 copy 表与正式表（DBUtil.exchange_tb 内部做 rename 交换）
    # 与 dwt_aba_last365 同款：cp_index_flag=True 复制索引到新正式表
    DBUtil.exchange_tb(
        engine,
        source_tb_name=export_tb_copy,
        target_tb_name=export_tb_target,
        cp_index_flag=True
    )

    # 6) 交换完成后，把正式表的 theme_ch_list 从 VARCHAR 转回 VARCHAR[]
    # USING string_to_array(...) 把 "材质,颜色" 拆成 {材质,颜色}
    # 词典无匹配的词 PySpark 已 fillna "-1"，转换后是 {-1}，与 Java 占位约定一致
    with engine.connect() as connection:
        sql = f"""
            ALTER TABLE {export_tb_target}
            ALTER COLUMN theme_ch_list TYPE VARCHAR[]
            USING string_to_array(theme_ch_list, ',')::varchar[];
        """
        print("================================执行 SQL================================")
        print(sql)
        connection.execute(sql)

    # 7) 更新 workflow_everyday 流程表（业务监听导出完成的标记）
    # 参考 sqoop_export/dwt_aba_last365.py 的写法
    # if test_flag != 'test':
    #     mysql_engine = DBUtil.get_db_engine("mysql", "us")
    #     with mysql_engine.connect() as connection:
    #         sql = f"""
    #             replace into workflow_everyday (
    #                 site_name, report_date, status, status_val, table_name, date_type, page, is_end, remark, export_db_type
    #             )
    #             values (
    #                 '{site_name}', '{date_info}', '导出pg完成', 14,
    #                 '{export_tb_target}', '365_day', 'AbaWordHeatYear', '是',
    #                 'ABA词频热度年表(最近12月,每月更新)', 'postgresql_cluster'
    #             );
    #         """
    #         print("================================更新 workflow_everyday================================")
    #         print(sql)
    #         connection.execute(sql)

    print("success")
