/**
 * 多平台账户凭据管理系统 - 核心类型定义 (专注Pacifica)
 * 单一职责：凭据分类管理和签名服务
 */

export type PlatformType = 'pacifica' | 'aster' | 'binance' | 'okx'

export type EnvironmentType = 'development' | 'production'

export type SignatureAlgorithm = 'ed25519' | 'eip191' | 'hmac-sha256'

export type SignatureEncoding = 'base58' | 'hex' | 'base64'

/**
 * 平台账户凭据接口
 */
export interface PlatformAccount {
  accountId: string
  platformId: PlatformType
  alias?: string

  // 加密存储的凭据（根据平台不同）
  credentials: {
    // Pacifica: Ed25519 私钥
    privateKey?: string // base58 编码

    // Aster: 以太坊私钥 + 签名者地址
    ethPrivateKey?: string // 0x 开头
    signerAddress?: string // 0x 开头

    // Binance/OKX: API 凭据
    apiKey?: string
    secretKey?: string
    passphrase?: string // OKX 需要
  }

  // 状态信息
  status: 'active' | 'inactive'
  environment: EnvironmentType

  // 使用统计
  usage: {
    totalSigns: number
    lastSignAt?: Date
    errorCount: number
  }

  createdAt: Date
  updatedAt: Date
}

/**
 * 签名结果接口
 */
export interface SignatureResult {
  signature: string
  algorithm: SignatureAlgorithm
  encoding: SignatureEncoding
  publicKey?: string // 如果适用
  signedAt: string // ISO timestamp
  metadata?: Record<string, any>
}

/**
 * 签名适配器接口
 */
export interface SignatureAdapter {
  platformId: PlatformType
  algorithm: SignatureAlgorithm

  // 签名方法
  sign(data: any, credentials: any): Promise<SignatureResult>

  // 验证方法（可选）
  verify?(signature: string, data: any, publicKey: string): Promise<boolean>
}

/**
 * 配置管理器接口
 */
export interface ConfigManager {
  configPath: string
  lastLoaded: Date

  // 加载的账户数据
  accounts: Map<string, PlatformAccount> // key: platformId:accountId

  // 配置操作
  loadConfig(): Promise<void>
  reloadConfig(): Promise<void>
  watchConfig(): void // 监控文件变化

  // 账户查询
  getAccount(platformId: string, accountId: string): PlatformAccount | null
  getAccountsByPlatform(platformId: string): PlatformAccount[]
}

/**
 * 主凭据管理器接口
 */
export interface CredentialManager {
  // 签名服务（核心功能）
  sign(platformId: string, accountId: string, data: any): Promise<SignatureResult>

  // 账户查询
  getAccount(platformId: string, accountId: string): PlatformAccount | null
  listAccounts(platformId?: string): PlatformAccount[]

  // 配置管理
  reloadConfig(): Promise<void>
  getStatus(): ConfigStatus
}

/**
 * 配置状态接口
 */
export interface ConfigStatus {
  configPath: string
  lastLoaded: Date
  accountCount: number
  platformCount: number
  isWatching: boolean
}

/**
 * Pacifica 签名数据格式
 */
export interface PacificaSignData {
  instruction: {
    type: string
    market?: string
    side?: string
    amount?: string
    price?: string
    [key: string]: any
  }
  nonce?: number
}

/**
 * 批量签名请求
 */
export interface BatchSignRequest {
  requests: Array<{
    requestId: string
    platformId: PlatformType
    accountId: string
    data: any
  }>
}

/**
 * 批量签名响应
 */
export interface BatchSignResponse {
  results: Array<{
    requestId: string
    status: 'success' | 'error'
    signature?: string
    algorithm?: SignatureAlgorithm
    error?: string
  }>
  summary: {
    total: number
    successful: number
    failed: number
  }
}

/**
 * API 错误代码
 */
export enum ErrorCode {
  ACCOUNT_NOT_FOUND = 'ACCOUNT_NOT_FOUND',
  PLATFORM_NOT_SUPPORTED = 'PLATFORM_NOT_SUPPORTED',
  INVALID_DATA_FORMAT = 'INVALID_DATA_FORMAT',
  SIGNATURE_FAILED = 'SIGNATURE_FAILED',
  CONFIG_LOAD_FAILED = 'CONFIG_LOAD_FAILED',
  ACCOUNT_INACTIVE = 'ACCOUNT_INACTIVE',
}

/**
 * API 错误响应
 */
export interface ApiError {
  code: ErrorCode
  message: string
  details?: Record<string, any>
}

/**
 * 标准 API 响应格式
 */
export interface ApiResponse<T = any> {
  success: boolean
  data?: T
  error?: ApiError
}
