from __future__ import annotations

from typing import Any, Dict

# ------------------------------------------------------------------------------
# 明道云：worksheetId 常量
# ------------------------------------------------------------------------------
MDY_WORKSHEET_ID_PRODUCT = "spmx"
MDY_WORKSHEET_ID_CUSTOMER = "quanbu"
MDY_WORKSHEET_ID_FABRIC = "668ba100fb551c850214067d"
MDY_WORKSHEET_ID_PLATE_ORDER = "668ba100fb551c8502140660"  # 开版数据表

# 开版表关联数据 worksheetId（用于 Relation 关联查询）
MDY_WORKSHEET_ID_PLATE_ORDER_DRAWING = "668ba100fb551c850214066b"  # 画图
MDY_WORKSHEET_ID_PLATE_ORDER_COLORING = "668ba100fb551c850214066c"  # 调色
MDY_WORKSHEET_ID_PLATE_ORDER_PATTERN_SET = "668ba100fb551c8502140681"  # 套纸样
MDY_WORKSHEET_ID_PLATE_ORDER_MODIFY_DRAWING = "66e2eb5a3c1a53053f6d234d"  # 改图
MDY_WORKSHEET_ID_PLATE_ORDER_COLOR_SCHEME = "672adc61e72ce9924f763a0b"  # 配色
MDY_WORKSHEET_ID_PLATE_ORDER_IMAGE_DEVELOPMENT = "66e2eee45d7e45f9c5ae41b4"  # 照图开发


mdy_table_map = {
    "product": MDY_WORKSHEET_ID_PRODUCT,
    "customer": MDY_WORKSHEET_ID_CUSTOMER,
    "plate_order": MDY_WORKSHEET_ID_PLATE_ORDER,
}

# 开版表关联数据表映射（后续做跨表查询时使用）
plate_order_related_worksheet_map = {
    "drawing": MDY_WORKSHEET_ID_PLATE_ORDER_DRAWING,
    "coloring": MDY_WORKSHEET_ID_PLATE_ORDER_COLORING,
    "pattern_set": MDY_WORKSHEET_ID_PLATE_ORDER_PATTERN_SET,
    "modify_drawing": MDY_WORKSHEET_ID_PLATE_ORDER_MODIFY_DRAWING,
    "color_scheme": MDY_WORKSHEET_ID_PLATE_ORDER_COLOR_SCHEME,
    "image_development": MDY_WORKSHEET_ID_PLATE_ORDER_IMAGE_DEVELOPMENT,
}

# 同上（中文 key，便于排查/对照）
plate_order_related_worksheet_map_cn = {
    "画图": MDY_WORKSHEET_ID_PLATE_ORDER_DRAWING,
    "调色": MDY_WORKSHEET_ID_PLATE_ORDER_COLORING,
    "套纸样": MDY_WORKSHEET_ID_PLATE_ORDER_PATTERN_SET,
    "改图": MDY_WORKSHEET_ID_PLATE_ORDER_MODIFY_DRAWING,
    "配色": MDY_WORKSHEET_ID_PLATE_ORDER_COLOR_SCHEME,
    "照图开发": MDY_WORKSHEET_ID_PLATE_ORDER_IMAGE_DEVELOPMENT,
}

# ------------------------------------------------------------------------------
# 明道云：开版相关“关联表”字段映射（用于后续关联查询/解析）
#
# 说明：
# - 这里记录的是“字段 controlId -> 字段含义/类型/备注”。
# - Relation(29) 字段在写入时通常按文档要求使用 rowId 字符串（多条用逗号分割、全量覆盖），
#   但 getFilterRows 拉取时返回结构可能是 list[{"rowid","name","link"}]，解析时需兼容两种形态。
# ------------------------------------------------------------------------------
plate_order_related_field_definitions: Dict[str, Dict[str, Any]] = {
    # 1) 画图
    "drawing": {
        "worksheet_id": MDY_WORKSHEET_ID_PLATE_ORDER_DRAWING,
        "name": "画图",
        "fields": {
            "62d7f09ad55983029b644b25": {"name": "设计师名字", "type": "Text"},
            "62d52f4b8d2972284492dda0": {"name": "电脑位置", "type": "Text"},
            "62d52f4b8d2972284492dd9b": {
                "name": "附件",
                "type": "Attachment",
                "note": "json，支持外部链接和 Base64 文件流",
            },
            "6412d650120c799be027953d": {
                "name": "开版管理",
                "type": "Relation",
                "note": "String 字符串，多条记录 rowId 用(,)分割，全量覆盖操作",
            },
            "62d52f4b8d2972284492dd9d": {
                "name": "开发管理",
                "type": "Relation",
                "note": "String 字符串，多条记录 rowId 用(,)分割，全量覆盖操作",
            },
        },
    },
    # 2) 调色
    "coloring": {
        "worksheet_id": MDY_WORKSHEET_ID_PLATE_ORDER_COLORING,
        "name": "调色",
        "fields": {
            "62d7f0c2d121077a8492619d": {"name": "设计师名字", "type": "Text"},
            "62d52f4b8d2972284492dda8": {"name": "电脑位置", "type": "Text"},
            "62d52f4b8d2972284492dda3": {
                "name": "附件",
                "type": "Attachment",
                "note": "json，支持外部链接和 Base64 文件流",
            },
            "6716620b985d1a2af61776b5": {
                "name": "完成数量",
                "type": "Number",
                "note": "Double（例如 666.66）",
            },
            "62d52f4b8d2972284492dda5": {
                "name": "开发管理",
                "type": "Relation",
                "note": "String 字符串，多条记录 rowId 用(,)分割，全量覆盖操作",
            },
        },
    },
    # 3) 套纸样
    "pattern_set": {
        "worksheet_id": MDY_WORKSHEET_ID_PLATE_ORDER_PATTERN_SET,
        "name": "套纸样",
        "fields": {
            "62d7f0cfa72269e1965389e7": {"name": "设计师名字", "type": "Text"},
            "62d52f4b8d2972284492de8c": {"name": "电脑位置", "type": "Text"},
            "62d52f4b8d2972284492de89": {
                "name": "附件",
                "type": "Attachment",
                "note": "json，支持外部链接和 Base64 文件流",
            },
            "671661bc8d15ca93fdec7e5f": {
                "name": "完成数量",
                "type": "Number",
                "note": "Double（例如 666.66）",
            },
            "62d52f4b8d2972284492de8d": {
                "name": "开版管理",
                "type": "Relation",
                "note": "String 字符串，多条记录 rowId 用(,)分割，全量覆盖操作",
            },
        },
    },
    # 4) 改图
    "modify_drawing": {
        "worksheet_id": MDY_WORKSHEET_ID_PLATE_ORDER_MODIFY_DRAWING,
        "name": "改图",
        "fields": {
            "62d7f0cfa72269e1965389e7": {"name": "设计师名字", "type": "Text"},
            "62d52f4b8d2972284492de8c": {"name": "电脑位置", "type": "Text"},
            "62d52f4b8d2972284492de89": {
                "name": "附件",
                "type": "Attachment",
                "note": "json，支持外部链接和 Base64 文件流",
            },
            "66e2ebb7da66655f355bf709": {
                "name": "开版管理",
                "type": "Relation",
                "note": "String 字符串，多条记录 rowId 用(,)分割，全量覆盖操作",
            },
        },
    },
    # 5) 配色
    "color_scheme": {
        "worksheet_id": MDY_WORKSHEET_ID_PLATE_ORDER_COLOR_SCHEME,
        "name": "配色",
        "fields": {
            "62d7f09ad55983029b644b25": {"name": "设计师名字", "type": "Text"},
            "62d52f4b8d2972284492dda0": {"name": "电脑位置", "type": "Text"},
            "62d52f4b8d2972284492dd9b": {
                "name": "附件",
                "type": "Attachment",
                "note": "json，支持外部链接和 Base64 文件流",
            },
            "672adc9b156abb9a08ab2a61": {
                "name": "开版管理",
                "type": "Relation",
                "note": "String 字符串，多条记录 rowId 用(,)分割，全量覆盖操作",
            },
            # 注：配色表无“完成数量”，用“开发数量”替代
            "66e2ef8d99632ae7376e73ef": {
                "name": "开发数量",
                "type": "Number",
                "note": "Double（例如 666.66）",
            },
        },
    },
    # 6) 找图开发（照图开发）
    "image_development": {
        "worksheet_id": MDY_WORKSHEET_ID_PLATE_ORDER_IMAGE_DEVELOPMENT,
        "name": "找图开发",
        "fields": {
            "62d7f09ad55983029b644b25": {"name": "设计师名字", "type": "Text"},
            "62d52f4b8d2972284492dda0": {"name": "电脑位置", "type": "Text"},
            "62d52f4b8d2972284492dd9b": {
                "name": "附件",
                "type": "Attachment",
                "note": "json，支持外部链接和 Base64 文件流",
            },
            "66e2efb0da66655f355bf963": {
                "name": "开版管理",
                "type": "Relation",
                "note": "String 字符串，多条记录 rowId 用(,)分割，全量覆盖操作",
            },
            # 注：找图开发表无“完成数量”，用“开发数量”替代
            "66e2ef8d99632ae7376e73ef": {
                "name": "开发数量",
                "type": "Number",
                "note": "Double（例如 666.66）",
            },
        },
    },
}


# ------------------------------------------------------------------------------
# 明道云：字段映射（内部字段名 -> controlId）
# ------------------------------------------------------------------------------
product_type_map = {
    "name": "668caa5eb80969563ecaee7d",
    "pieces": "689c5aa37c175367b29fcb87",
    "color": "66ed3b9ae01d5599bdb45f6d",
    "uid": "668caa5eb80969563ecaee7c",
    "unit": "668e2ee2ca758c8cc5d0dc1d",
    "segment_size": "668e2194790c0e04058b4f3c",
    "width": "668e2194790c0e04058b4f3c",
    "detail_str": "668caa5eb80969563ecaee7e",
    "created_at": "ctime",
    "rowid": "rowid",
}

customer_type_map = {
    "name": "62d52f4b8d2972284492dcf9",
    "area": "62d52f4b8d2972284492dd09",
    "created_at": "ctime",
    "rowid": "rowid",
    "uid": "668bb9370207cf7520fe551e",
}

fabric_type_map = {
    "name": "62d52f4b8d2972284492de61",
}


# ------------------------------------------------------------------------------
# 明道云：开版数据表（worksheetId: 668ba100fb551c8502140660）
# 字段说明（controlId -> 元信息）。按需求保留为 dict，暂不用于拉取请求。
# ------------------------------------------------------------------------------
plate_order_field_definitions: Dict[str, Dict[str, Any]] = {
    # 成员 / 人员
    "62d8fffb625ac34fa91299c3": {"name": "调色", "type": "Collaborator"},
    # 关联记录（Relation）—— 展现 rowid + name + link
    "62d52f4b8d2972284492dd28": {"name": "打版面料", "type": "Relation"},
    "62d52f4b8d2972284492dd14": {"name": "客户", "type": "Relation"},
    # 下拉/文本/数值/日期
    "62d52f4b8d2972284492dd15": {"name": "做货方式", "type": "Dropdown"},
    "62d52f4b8d2972284492dd0e": {"name": "设计编号", "type": "AutoNumber"},
    "62d91d0e130624d31368bd16": {"name": "开版方式", "type": "Dropdown"},
    "64a64bc4402bb5226ee34b3c": {"name": "等级", "type": "Rating"},
    "660b79da4125de62cd904b39": {"name": "布料", "type": "Dropdown"},
    "62db8e78510fb7962439b19c": {"name": "调色设计师", "type": "Text"},
    "62d52f4b8d2972284492dd10": {"name": "开发进程", "type": "Dropdown"},
    "62d52f4b8d2972284492dd1e": {"name": "是否套唛架", "type": "Dropdown"},
    "62d52f4b8d2972284492dd2c": {"name": "款号名称", "type": "Text"},
    "62d52f4b8d2972284492dd1d": {"name": "审批结果", "type": "Dropdown"},
    "62d52f4b8d2972284492dd12": {"name": "下版时间", "type": "DateTime"},
    "62d52f4b8d2972284492dd18": {"name": "复版原因", "type": "Dropdown"},
    "62d52f4b8d2972284492dd0f": {"name": "起版情况", "type": "Dropdown"},
    "62d52f4b8d2972284492dd19": {"name": "客户要求米样米数", "type": "Number"},
    "62d52f4b8d2972284492dd11": {"name": "紧急程度", "type": "Dropdown"},
    "660b75c3422f028085086b3c": {"name": "幅宽", "type": "Dropdown"},
    "62d52f4b8d2972284492dd13": {"name": "要求完成时间", "type": "Date"},
    "62f72a26d73f8581fbe7a488": {"name": "难度评级", "type": "Dropdown"},
    # 附件（Attachment）—— 展现文件名 + 下载链接
    "62d52f4b8d2972284492dd27": {"name": "开版图", "type": "Attachment"},
    # 评级/开关/组合字段
    "6451f4ad25766313eb8cb057": {"name": "日期", "type": "DateTime"},
    "66c2ebc8666ad6264b709fa1": {"name": "画图评级", "type": "Dropdown"},
    "66c2ebc8666ad6264b709fa2": {"name": "调色评级", "type": "Dropdown"},
    "66c2ebc8666ad6264b709fa3": {"name": "套样评级", "type": "Dropdown"},
    "6718ac791f08f8ec31257ebc": {"name": "记录id（文本组合）", "type": "Concatenate"},
    "67f4d7d61c5f650e952fcee9": {"name": "客户名称", "type": "Lookup"},
    # “有这个字段，但本条记录可能为空”的 Relation（占位：[]）
    "62d52f4b8d2972284492dd2d": {"name": "套纸样", "type": "Relation"},
    "62d52f4b8d2972284492dd21": {"name": "画图", "type": "Relation"},
    "62d52f4b8d2972284492dd22": {"name": "调色（关联）", "type": "Relation"},
    "62d52f4b8d2972284492dd2a": {"name": "套纸样（关联）", "type": "Relation"},
    "66e2ebb7da66655f355bf708": {"name": "改图（关联）", "type": "Relation"},
    "66e2efb0da66655f355bf964": {"name": "找图开发", "type": "Relation"},
    "672adc9b156abb9a08ab2a60": {"name": "配色", "type": "Relation"},
}

# 同步代码风格：内部字段名 -> controlId（后续 pick / parse 时会用到）
plate_order_type_map = {
    "colorist": "62d8fffb625ac34fa91299c3",
    "fabric_relation": "62d52f4b8d2972284492dd28",
    "customer_relation": "62d52f4b8d2972284492dd14",
    "production_method": "62d52f4b8d2972284492dd15",
    "design_no": "62d52f4b8d2972284492dd0e",
    "plate_method": "62d91d0e130624d31368bd16",
    "level_rating": "64a64bc4402bb5226ee34b3c",
    "fabric_source": "660b79da4125de62cd904b39",
    "color_designer": "62db8e78510fb7962439b19c",
    "dev_progress": "62d52f4b8d2972284492dd10",
    "need_marker_frame": "62d52f4b8d2972284492dd1e",
    "style_name": "62d52f4b8d2972284492dd2c",
    "approval_result": "62d52f4b8d2972284492dd1d",
    "plate_time": "62d52f4b8d2972284492dd12",
    "rework_reason": "62d52f4b8d2972284492dd18",
    "start_plate_status": "62d52f4b8d2972284492dd0f",
    "sample_meters": "62d52f4b8d2972284492dd19",  # 对应样品米数（宇问云）
    "urgency": "62d52f4b8d2972284492dd11",
    "width": "660b75c3422f028085086b3c",
    "required_finish_date": "62d52f4b8d2972284492dd13",
    "difficulty_level": "62f72a26d73f8581fbe7a488",
    "plate_images": "62d52f4b8d2972284492dd27",
    "record_datetime": "6451f4ad25766313eb8cb057",
    "drawing_rating": "66c2ebc8666ad6264b709fa1",
    "color_rating": "66c2ebc8666ad6264b709fa2",
    "pattern_fit_rating": "66c2ebc8666ad6264b709fa3",
    "record_id": "6718ac791f08f8ec31257ebc",
    "customer_name": "67f4d7d61c5f650e952fcee9",
    # relations（可能为空）
    "pattern_set": "62d52f4b8d2972284492dd2d",
    "drawing_relation": "62d52f4b8d2972284492dd21",
    "color_relation": "62d52f4b8d2972284492dd22",
    "pattern_set_relation": "62d52f4b8d2972284492dd2a",
    "modify_drawing_relation": "66e2ebb7da66655f355bf708",
    "image_development_relation": "66e2efb0da66655f355bf964",
    "color_scheme_relation": "672adc9b156abb9a08ab2a60",
    # 系统字段
    "created_at": "ctime",
    "rowid": "rowid",
}

# PlateOrder 行中“关联字段 controlId -> 关联表 key”
# 用于从开版主表记录中提取跨表 rowid 并进一步查询关联表数据。
plate_order_relation_control_map = {
    plate_order_type_map["drawing_relation"]: "drawing",
    plate_order_type_map["color_relation"]: "coloring",
    plate_order_type_map["pattern_set"]: "pattern_set",
    plate_order_type_map["modify_drawing_relation"]: "modify_drawing",
    plate_order_type_map["color_scheme_relation"]: "color_scheme",
    plate_order_type_map["image_development_relation"]: "image_development",
}

# json key 查询
# MDYPlateOrderStaging.objects.filter(
#     raw__62d52f4b8d2972284492dd0e="82724"  # 设计编号/订单id（明道云字段ID）
# )
