export const COS_BUCKET: string = 'plates-1251306435';
export const COS_REGION: string = 'ap-guangzhou';
export const COS_PUBLIC_BASE: string = '';

// 统一的对象键前缀（可按业务拆分）
export const COS_KEY_PREFIX = 'uploads/';

// 生成文件名（含子目录），避免冲突
export function buildCosKey(params: { prefix?: string; ext?: string; userId?: string }): string {
  const { prefix, ext, userId } = params;
  const date = new Date();
  const yyyy = date.getFullYear();
  const mm = String(date.getMonth() + 1).padStart(2, '0');
  const dd = String(date.getDate()).padStart(2, '0');
  const ts = date.getTime();
  const rand = Math.random().toString(36).slice(2, 8);
  const base = `${COS_KEY_PREFIX}${yyyy}/${mm}/${dd}/${userId ? userId + '/' : ''}${ts}_${rand}`;
  return `${prefix ? prefix.replace(/\/*$/, '/') : ''}${base}${ext ? (ext.startsWith('.') ? ext : `.${ext}`) : ''}`;
}

export function buildPublicUrl(key: string): string {
  const cleanedKey = key.replace(/^\//, '');
  if (COS_PUBLIC_BASE && COS_PUBLIC_BASE.trim()) {
    return `${COS_PUBLIC_BASE.replace(/\/$/, '')}/${cleanedKey}`;
  }
  // 回退：使用 COS 默认公网域名
  // 例如: https://<bucket>.cos.<region>.myqcloud.com/<key>
  if (COS_BUCKET && COS_REGION) {
    return `https://${COS_BUCKET}.cos.${COS_REGION}.myqcloud.com/${cleanedKey}`;
  }
  return '';
}


