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

"""
系统内部服务API配置
用于配置与系统内部服务通信的相关参数
"""

import os
from typing import Dict, Any, Optional, List

# 天系统内部服务API端点配置
tf_api_endpoints = {
    "login": "/sys/ext/login",
    "create_task": "/task",
    "choose_amr": "/taskBlock",
    "add_action": "/taskBlockAction",
    "closure_task": "/taskBlock/{}/closure",
    "get_task_block": "/taskBlock/{id}",
    "get_task_block_action": "/taskBlockAction/{id}",
    "set_task_in_progress": "/task/vwedtask/{id}/inprogress",
    "set_task_completed": "/task/vwedtask/{id}/completed",
    "set_task_terminated": "/task/vwedtask/{id}/terminated",
    "set_task_failed": "/task/vwedtask/{id}/failed",
    "set_task_description": "/task/vwedtask/{id}/description",
    "get_amr_info": "/task/vwedtask/getAmrInfo"
}

# 系统内部服务API HTTP方法配置
tf_api_methods = {
    "login": "POST",
    "create_task": "POST",
    "choose_amr": "POST",
    "add_action": "POST",
    "closure_task": "PUT",
    "get_task_block": "GET",
    "get_task_block_action": "GET",
    "set_task_in_progress": "PUT",
    "set_task_completed": "PUT",
    "set_task_terminated": "PUT",
    "set_task_failed": "PUT",
    "set_task_description": "PUT",
    "get_amr_info": "GET"
}
# 外部任务类型对应的优先级
TASK_TYPE_PRIORITY={
    "GT": "6", # 缸体
    "GG": "5", # 缸盖
    "QZ": "4", # 曲轴
    "ZG": "3",#
    "PHZ": "2",
    "LG": "1",
    "OR": "1"
}
# 外部任务对应所属库区
TASK_TYPE_AREA={
    "GT": "ZK/ZKG",
    "GG": "ZK/ZKG",
    "QZ": "KW",
    "ZG": "ZK/ZKG",
    "PHZ": "AGW/PL",
    "LG": "AGW/PL"
}
# 从环境变量读取配置，或使用默认值
TF_API_BASE_URL = os.getenv("TF_API_BASE_URL", "http://192.168.189.206:8080/jeecg-boot")
# TF_API_BASE_URL = os.getenv("TF_API_BASE_URL", "http://111.231.146.230:4080/jeecg-boot")
TF_API_TIMEOUT = int(os.getenv("TF_API_TIMEOUT", "10"))  # 减少超时时间从60秒到10秒
TF_API_RETRY_TIMES = int(os.getenv("TF_API_RETRY_TIMES", "3"))
TF_API_MOCK_MODE = False
TF_API_TOKEN_HEADER = os.getenv("TF_API_TOKEN_HEADER", "X-Access-Token") # token请求头名称
TF_API_TOKEN = os.getenv("TF_API_TOKEN", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3NDk3NzY1MzEsInVzZXJuYW1lIjoiYWRtaW4ifQ.uRLHZuRQTrR2fHyA-dMzP46yXAa5wdjfdUcmr9PNY4g")
TF_WEB_POST = os.getenv("TF_WEB_POST", "false").lower() == "true"  # 是否过账，从环境变量读取
sync_disabled_label = "sync_disabled" # 是否限制接口调用的变量名

# MQTT服务配置
MQTT_CONFIG = {
    "host": os.getenv("MQTT_HOST", "192.168.189.97"),
    "port": int(os.getenv("MQTT_PORT", "1883")),
    "username": os.getenv("MQTT_USERNAME", ""),
    "password": os.getenv("MQTT_PASSWORD", ""),
    "keepalive": int(os.getenv("MQTT_KEEPALIVE", "60")),
    "reconnect_delay": int(os.getenv("MQTT_RECONNECT_DELAY", "5")),
    "max_retries": int(os.getenv("MQTT_MAX_RETRIES", "3"))
}
# 外部接口叫料模板id
CM_ID = os.getenv("CM_ID", "571985c1-cfa5-4186-8acd-6e3868a5e08c")
# 送货模板id
DG_ID = os.getenv("DG_ID", "e22cacb4-a580-45ba-949e-356f57fa1a43")

# MQTT Topic配置
MQTT_TOPIC_CONFIG = {
    # 实例ID配置
    "instance_id": os.getenv("MQTT_INSTANCE_ID", "asbm2"),

    # 支持的设备品牌配置 - 按设备类型分类
    "supported_brands": {
        # 小车(vehicle)设备品牌
        "vehicle": {
            "huarui": {
                "name": "华瑞",
                "brand_suffix": "_IRAYPLE",
                "oagv_version": "v2",
                "uagv_version": "v2",
                "device_type": "vehicle"
            },
            "seer": {
                "name": "仙工",
                "brand_suffix": "_SEER",
                "oagv_version": "v2",
                "uagv_version": "v2",
                "device_type": "vehicle"
            },
        },
        # 其他设备品牌（门、光电传感器、呼叫器等）
        "other": {
            "door": {
                "name": "门控制器",
                "brand_suffix": "_VWED",
                "oagv_version": "v2",
                "uagv_version": "2.0.0",
                "device_type": "door"
            },
            "sensor": {
                "name": "光电传感器",
                "brand_suffix": "_SENSOR",
                "oagv_version": "v2",
                "uagv_version": "v2",
                "device_type": "sensor"
            },
            # "caller": {
            #     "name": "呼叫器",
            #     "brand_suffix": "_CALLER",
            #     "oagv_version": "v2",
            #     "uagv_version": "v2",
            #     "device_type": "caller"
            # },
            # "lift": {
            #     "name": "电梯",
            #     "brand_suffix": "_LIFT",
            #     "oagv_version": "v2",
            #     "uagv_version": "v2",
            #     "device_type": "lift"
            # },
            # "conveyor": {
            #     "name": "输送带",
            #     "brand_suffix": "_CONVEYOR",
            #     "oagv_version": "v2",
            #     "uagv_version": "v2",
            #     "device_type": "conveyor"
            # }
        }
    },

    # 默认监听的指令类型
    "default_command_types": {
        # vehicle类型设备默认监听的指令（小车专有指令）
        "vehicle": ["order", "instantActions", "factsheet"],
        # other类型设备默认监听的指令（其他设备只支持即时动作）
        "other": ["instantActions"]
    },

    # 指令方向配置
    "command_directions": {
        "order": {"listen": "oagv", "forward": "uagv"},
        "instantActions": {"listen": "oagv", "forward": "uagv"},
        "factsheet": {"listen": "oagv", "forward": "uagv"},
        "state": {"listen": "uagv", "forward": "oagv"}
    }
}
def get_tf_api_config() -> Dict[str, Any]:
    """获取天风系统API配置"""
    return {
        "base_url": TF_API_BASE_URL,
        "timeout": TF_API_TIMEOUT,
        "retry_times": TF_API_RETRY_TIMES,
        "mock_mode": TF_API_MOCK_MODE,
        "endpoints": tf_api_endpoints,
        "methods": tf_api_methods,
        "token_header": TF_API_TOKEN_HEADER
    } 