import { createHttpError } from '../../../utils/http.js'
import {
  normalizeCloudtentaclesDeviceId,
  normalizeCloudtentaclesDeviceType,
} from '../../platforms/cloudtentacles/defaults.js'
import { getCloudtentaclesSourceByKey } from '../../platforms/cloudtentacles/source-config-service.js'
import { getCloudtentaclesSessionStateByKey } from '../../platforms/cloudtentacles/session-state-service.js'
import { normalizeStringArray } from './domain.js'

/**
 * 严格模式：只解析列表中第一个账号（调用方均把任务实际取号账号 resolvedSourceKey 放首位），
 * 该账号不可用时直接报错，绝不静默 fallback 到其他账号——
 * 否则会用「非取号账号」去退号/取链，上游会因号码不归属当前会话而报权限不足。
 * 退号、取链、发货等「操作任务已有号码」的场景必须使用此函数。
 * 将实际使用的 resolvedSourceKey 也返回，确保后续操作使用同一个 sourceKey。
 */
export function resolvePersistedCloudtentaclesContextBySourceKeys(sourceKeys: unknown[] = []) {
  const candidates = [...new Set(normalizeStringArray(sourceKeys))]

  if (candidates.length === 0) {
    throw createHttpError('cloudtentacles 没有可用账号，请先到平台配置完成账号配置', {
      statusCode: 409,
      errorCode: 'kuaishou_cloud_no_source_keys',
    })
  }

  const sourceKey = String(candidates[0] || '').trim()

  return resolveSingleCloudtentaclesAccountContext(sourceKey)
}

/**
 * 宽松模式：按顺序尝试候选账号，找到第一个有可用 token 的账号。
 * 仅用于「取新号」场景（新号码归属被选中的账号，不会产生跨账号孤儿），
 * 以及 account-selector 单候选解析。严禁用于退号/取链/发货等操作已有号码的场景。
 */
export function resolvePersistedCloudtentaclesContextWithFallback(sourceKeys: unknown[] = []) {
  const candidates = [...new Set(normalizeStringArray(sourceKeys))]

  let lastError: unknown = null

  for (const sourceKey of candidates) {
    try {
      return resolveSingleCloudtentaclesAccountContext(sourceKey)
    } catch (error) {
      lastError = error
    }
  }

  throw (
    lastError ||
    createHttpError('所有 cloudtentacles 账号均不可用，请先到平台配置完成登录校验', {
      statusCode: 409,
      errorCode: 'kuaishou_cloud_all_source_keys_exhausted',
    })
  )
}

function resolveSingleCloudtentaclesAccountContext(sourceKey: string) {
  const source = getCloudtentaclesSourceByKey(sourceKey)
  const session = getCloudtentaclesSessionStateByKey(sourceKey)

  if (!source) {
    throw createHttpError(
      `cloudtentacles 账号 ${sourceKey} 不存在（取号账号已变更或被删除，无法继续操作其虚拟号）`,
      { statusCode: 409, errorCode: 'kuaishou_cloud_source_missing' },
    )
  }

  if (source.enabled === false) {
    throw createHttpError(`cloudtentacles 账号 ${sourceKey} 已停用`, {
      statusCode: 409,
      errorCode: 'kuaishou_cloud_source_disabled',
    })
  }

  if (!session) {
    throw createHttpError(`cloudtentacles 账号 ${sourceKey} 没有可用 token`, {
      statusCode: 409,
      errorCode: 'kuaishou_cloud_missing_cloud_token',
    })
  }

  const token = String(session.token || '').trim()
  if (!token) {
    throw createHttpError(`cloudtentacles 账号 ${sourceKey} 没有可用 token`, {
      statusCode: 409,
      errorCode: 'kuaishou_cloud_missing_cloud_token',
    })
  }

  return {
    baseUrl: String(session.baseUrl || source.baseUrl || '').trim() || 'https://123.207.217.176',
    token,
    deviceId: normalizeCloudtentaclesDeviceId(session.deviceId || source.deviceId),
    deviceType: normalizeCloudtentaclesDeviceType(session.deviceType ?? source.deviceType),
    // 透传给 http-client / 错误包装，便于定位跨账号问题
    sourceKey,
    resolvedSourceKey: sourceKey,
    accountLabel: String(source.label || sourceKey).trim() || sourceKey,
  }
}
