import logging
import os
import json
from dotenv import load_dotenv

logger = logging.getLogger(__name__)

# Load environment variables
load_dotenv()

# Load Storage Configuration
SETTINGS_DIR = os.path.dirname(os.path.abspath(__file__))
STORAGE_CONFIG_PATH = os.path.join(SETTINGS_DIR, 'storage.json')

storage_config = {}
if os.path.exists(STORAGE_CONFIG_PATH):
    try:
        with open(STORAGE_CONFIG_PATH, 'r') as f:
            storage_config = json.load(f)
    except Exception as e:
        logger.error(f"Failed to load storage config: {e}")

# 服务器 Configuration
PY_PORT = int(os.getenv('PY_PORT', '8000'))
NODE_ENV = os.getenv('NODE_ENV', 'development')

# CORS Configuration
ALLOWED_ORIGINS = [o for o in os.getenv('CORS_ALLOWED_ORIGINS', '').split(',') if o]
DEV_ALLOWED_ORIGINS = [
    o for o in os.getenv(
        'CORS_DEV_ALLOWED_ORIGINS',
        'http://localhost:3000,http://127.0.0.1:3000,http://localhost:3001,http://127.0.0.1:3001'
    ).split(',') if o
]
ALLOW_DEV_ORIGINS = (os.getenv('ALLOW_DEV_ORIGINS', '1') != '0' and NODE_ENV != 'production')

# 数据库 Configuration
DATA_DIR = os.getenv('DATA_DIR') or os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'data'))
DB_PATH = os.getenv('DB_PATH') or os.path.join(DATA_DIR, 'pixel.db')
DATABASE_URL = os.getenv('DATABASE_URL')

# Alibaba Cloud OSS Configuration (env vars take precedence over storage.json)
OSS_REGION = os.getenv('OSS_REGION') or storage_config.get('OSS_REGION', 'oss-cn-shanghai')
OSS_ENDPOINT = os.getenv('OSS_ENDPOINT') or storage_config.get('OSS_ENDPOINT', 'oss-cn-shanghai.aliyuncs.com')

OSS_BUCKET = os.getenv('OSS_BUCKET') or storage_config.get('OSS_BUCKET')
ALIBABA_CLOUD_ACCESS_KEY_ID = os.getenv('ALIBABA_CLOUD_ACCESS_KEY_ID') or storage_config.get('ALIBABA_CLOUD_ACCESS_KEY_ID')
ALIBABA_CLOUD_ACCESS_KEY_SECRET = os.getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET') or storage_config.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET')

# DashScope Configuration (Qwen, Wanx)
DASHSCOPE_API_KEY = os.getenv('DASHSCOPE_API_KEY')

# 模型Scope Configuration
MODELSCOPE_API_TOKEN = os.getenv('MODELSCOPE_API_TOKEN')

# Volcengine Configuration
VOLCENGINE_API_KEY = os.getenv('VOLCENGINE_API_KEY')  # 火山方舟 (LLM)

# Google Configuration
GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')

# OpenAI Configuration
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
OPENAI_BASE_URL = os.getenv('OPENAI_BASE_URL') # 可选: for proxies

# MiniMax Configuration
MINIMAX_API_KEY = os.getenv('MINIMAX_API_KEY')
MINIMAX_GROUP_ID = os.getenv('MINIMAX_GROUP_ID') # Sometimes needed

# Kling AI Configuration
KLING_ACCESS_KEY = os.getenv('KLING_ACCESS_KEY')
KLING_SECRET_KEY = os.getenv('KLING_SECRET_KEY')
KLING_API_BASE = os.getenv('KLING_API_BASE', 'https://api-beijing.klingai.com/v1')

# Midjourney Configuration (Youchuan / Proxy)
MIDJOURNEY_API_KEY = os.getenv('MIDJOURNEY_API_KEY')
MIDJOURNEY_PROXY_URL = os.getenv('MIDJOURNEY_PROXY_URL')
YOUCHUAN_APP_ID = os.getenv('YOUCHUAN_APP_ID')
YOUCHUAN_SECRET_KEY = os.getenv('YOUCHUAN_SECRET_KEY')

# Application Settings
UPLOAD_DIR = os.path.join(DATA_DIR, 'uploads')

# 存储 Configuration
STORAGE_TYPE = os.getenv('STORAGE_TYPE') or storage_config.get('STORAGE_TYPE', 'local')  # 'local' or 'oss'
PROJECTS_DIR = os.path.join(DATA_DIR, "projects")
CANVAS_DIR = os.path.join(DATA_DIR, "canvas")

# Redis Configuration
REDIS_URL = os.getenv('REDIS_URL', 'redis://localhost:6379')
REDIS_ENABLED = os.getenv('REDIS_ENABLED', '1') != '0'

# 追踪 Configuration
TRACING_ENABLED = os.getenv('TRACING_ENABLED', '0') != '0'
OTLP_ENDPOINT = os.getenv('OTLP_ENDPOINT', 'http://localhost:4317')

# Task Management Configuration
# Unified task manager is now always used
