import os

import pika
from dotenv import load_dotenv
from pydantic import BaseSettings

BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../'))
load_dotenv(os.path.join(BASE_DIR, '.env'))


class Settings(BaseSettings):
    PROJECT_NAME: str = 'FASTAPI BASE'
    SECRET_KEY: str = ''
    API_PREFIX: str = ''
    BACKEND_CORS_ORIGINS: list[str] = ['*']
    DATABASE_URL: str = ''
    ACCESS_TOKEN_EXPIRE_SECONDS: int = 60 * 60 * 24 * 7  # Token expired after 7 days
    SECURITY_ALGORITHM: str = 'HS256'
    LOGGING_CONFIG_FILE: str = os.path.join(BASE_DIR, 'logging_env.py')


OSS = "minio"
DEBUG = False
if DEBUG:
    LOGS_PATH = "logs/"
    CATEGORY_PATH = "service/attribute/config/descriptor/category/category_dis.csv"
    SEG_CACHE_PATH = "../seg_cache/"
    POSE_TRANSFORM_VIDEO_PATH = "../pose_transform_video/"
    RECOMMEND_PATH_PREFIX = "service/recommend/"
    CHROMADB_PATH = "./chromadb/"
else:
    LOGS_PATH = "app/logs/"
    CATEGORY_PATH = "app/service/attribute/config/descriptor/category/category_dis.csv"
    SEG_CACHE_PATH = "/seg_cache/"
    POSE_TRANSFORM_VIDEO_PATH = "/pose_transform_video/"
    RECOMMEND_PATH_PREFIX = "app/service/recommend/"
    CHROMADB_PATH = "/chromadb/"

# RABBITMQ_ENV = ""  # 生产环境
RABBITMQ_ENV = os.getenv("RABBITMQ_ENV", "-dev")
# RABBITMQ_ENV = "-local"  # 本地测试环境

if RABBITMQ_ENV == "-dev":
    JAVA_STREAM_API_URL = f"https://develop.api.aida.com.hk/api/third/party/receiveDesignResults"
elif RABBITMQ_ENV == "-prod":
    JAVA_STREAM_API_URL = f"https://api.aida.com.hk/api/third/party/receiveDesignResults"

settings = Settings()

# minio 配置
MINIO_URL = "www.minio-api.aida.com.hk"
MINIO_ACCESS = 'vXKFLSJkYeEq2DrSZvkB'
MINIO_SECRET = 'uKTZT3x7C43WvPN9QTc99DiRkwddWZrG9Uh3JVlR'
MINIO_SECURE = True

# S3 配置
S3_ACCESS_KEY = "AKIAVD3OJIMF6UJFLSHZ"
S3_AWS_SECRET_ACCESS_KEY = "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"
S3_REGION_NAME = "ap-east-1"

# redis 配置
REDIS_HOST = "10.1.1.240"
REDIS_PORT = "6379"
REDIS_DB = "2"

# rabbitmq config
RABBITMQ_PARAMS = {
    "host": "18.167.251.121",
    "port": 5672,
    "credentials": pika.credentials.PlainCredentials(username='rabbit', password='123456'),
    "virtual_host": "/"
}

# milvus 配置
MILVUS_URL = "http://10.1.1.240:19530"
MILVUS_TOKEN = "root:Milvus"
MILVUS_ALIAS = "default"
MILVUS_TABLE_KEYPOINT = "keypoint_cache_2"
MILVUS_TABLE_SEG = "seg_cache"

# Mysql 配置
DB_HOST = '18.167.251.121'  # 数据库主机地址
# DB_PORT = int( 33006)
DB_PORT = 33008  # 数据库端口
DB_USERNAME = 'aida_con'  # 数据库用户名
DB_PASSWORD = '123456'  # 数据库密码
DB_NAME = 'aida_back'  # 数据库库名

# openai
os.environ['SERPAPI_API_KEY'] = "a793513017b0718db7966207c31703d280d12435c982f1e67bbcbffa52e7632c"
OPENAI_STREAM = True
BUFFER_THRESHOLD = 6  # must be even number
SINGLE_TOKEN_THRESHOLD = 200
TOKEN_THRESHOLD = 600
OPENAI_TEMPERATURE = 0

# OPENAI_API_KEY =  "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"
OPENAI_API_KEY = "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"
OPENAI_MODEL = "gpt-3.5-turbo-0613"
OPENAI_MODEL_LIST = {"gpt-3.5-turbo-0613",
                     "gpt-3.5-turbo-16k-0613",
                     "gpt-4-0314",
                     "gpt-4-32k-0314",
                     "gpt-4-0613",
                     "gpt-4-32k-0613", }

# SR service config
SR_MODEL_NAME = "super_resolution"
SR_TRITON_URL = "10.1.1.240:10031"
SR_MINIO_BUCKET = "aida-users"
SR_RABBITMQ_QUEUES = f"SuperResolution{RABBITMQ_ENV}"

# GenerateImage service config
FAST_GI_MODEL_URL = '10.1.1.243:10011'
FAST_GI_MODEL_NAME = 'stable_diffusion_xl'

GI_MODEL_URL = '10.1.1.240:10061'
GI_MODEL_NAME = 'flux'

GMV_MODEL_URL = '10.1.1.243:10081'
GMV_MODEL_NAME = 'multi_view'

GMV_RABBITMQ_QUEUES = f"GenerateMultiView{RABBITMQ_ENV}"

GI_MINIO_BUCKET = "aida-users"
GI_RABBITMQ_QUEUES = f"GenerateImage{RABBITMQ_ENV}"
GI_SYS_IMAGE_URL = "aida-sys-image/generate_image/white_image.jpg"

# SLOGAN service config
SLOGAN_RABBITMQ_QUEUES = f"Slogan{RABBITMQ_ENV}"

# Generate Single Logo service config
GSL_MODEL_URL = '10.1.1.243:10041'
GSL_MINIO_BUCKET = "aida-users"
GSL_MODEL_NAME = 'stable_diffusion_xl_transparent'
GEN_SINGLE_LOGO_RABBITMQ_QUEUES = f"GenSingleLogo{RABBITMQ_ENV}"

# Generate Product service config
# GPI_RABBITMQ_QUEUES = os.getenv("GEN_PRODUCT_IMAGE_RABBITMQ_QUEUES", f"ToProductImage{RABBITMQ_ENV}")
# GPI_MODEL_NAME_OVERALL = 'sdxl_ensemble_all'
# GPI_MODEL_URL = '10.1.1.243:10051'

# Generate Product service config 旧版product img 模型
GPI_RABBITMQ_QUEUES = f"ToProductImage{RABBITMQ_ENV}"
BATCH_GPI_RABBITMQ_QUEUES = f"BatchToProductImage{RABBITMQ_ENV}"
GPI_MODEL_NAME_OVERALL = 'diffusion_ensemble_all'
GPI_MODEL_NAME_SINGLE = 'stable_diffusion_1_5_cnet'
GPI_MODEL_URL = '10.1.1.243:10051'

# Generate Single Logo service config
GRI_RABBITMQ_QUEUES = f"Relight{RABBITMQ_ENV}"
BATCH_GRI_RABBITMQ_QUEUES = f"BatchRelight{RABBITMQ_ENV}"
GRI_MODEL_NAME_OVERALL = 'diffusion_relight_ensemble'
GRI_MODEL_NAME_SINGLE = 'stable_diffusion_1_5_relight'
GRI_MODEL_URL = '10.1.1.240:10051'

# Pose Transform service config

PS_RABBITMQ_QUEUES = f"PoseTransform{RABBITMQ_ENV}"
BATCH_PS_RABBITMQ_QUEUES = f"BatchPoseTransform{RABBITMQ_ENV}"
PT_MODEL_URL = '10.1.1.243:10061'

# SEG service config
SEGMENTATION = {
    "new_model_name": "seg_knet",
    "name": "seg_ocrnet_hr18",
    "input": "seg_input__0",
    "output": "seg_output__0",
}
# ollama config
OLLAMA_URL = "http://10.1.1.240:11434/api/embeddings"

# design batch
BATCH_DESIGN_RABBITMQ_QUEUES = f"DesignBatch{RABBITMQ_ENV}"

# DESIGN config
DESIGN_MODEL_URL = '10.1.1.240:10000'
AIDA_CLOTHING = "aida-clothing"
KEYPOINT_RESULT_TABLE_FIELD_SET = ('neckline_left', 'neckline_right', 'shoulder_left', 'shoulder_right', 'armpit_left', 'armpit_right',
                                   'cuff_left_in', 'cuff_left_out', 'cuff_right_in', 'cuff_right_out', 'waistband_left', 'waistband_right')

# DESIGN 预处理
IF_DEBUG_SHOW = False

# 优先级
PRIORITY_DICT = {
    'earring_front': 99,
    'bag_front': 98,
    'hairstyle_front': 97,
    'outwear_front': 20,
    'tops_front': 19,
    'dress_front': 18,
    'blouse_front': 17,
    'skirt_front': 16,
    'trousers_front': 15,
    'bottoms_front': 14,
    'shoes_right': 1,
    'shoes_left': 1,
    'body': 0,
    'bottoms_back': -14,
    'trousers_back': -15,
    'skirt_back': -16,
    'blouse_back': -17,
    'dress_back': -18,
    'tops_back': -19,
    'outwear_back': -20,
    'hairstyle_back': -97,
    'bag_back': -98,
    'earring_back': -99,
}

QWEN_API_KEY = "sk-f31c29e61ac2498ba5e307aaa6dc10e0"

DB_CONFIG = {
    "host": "18.167.251.121",
    "port": 3306,
    "user": "root",
    "password": "QWa998345",
    "database": "aida",
    "charset": "utf8mb4"
}

TABLE_CATEGORIES = {
    "female_dress": "female/dress",
    "female_outwear": "female/outwear",
    "female_trousers": "female/trousers",
    "female_skirt": "female/skirt",
    "female_blouse": "female/blouse",
    "male_tops": "male/tops",
    "male_bottoms": "male/bottoms",
    "male_outwear": "male/outwear"
}

# --- ComfyUI 配置信息 ---
COMFYUI_SERVER_ADDRESS = "10.1.2.227:8080"  # 替换为您的 ComfyUI 服务器地址
