/**
 * 核心类型定义
 *
 * 包含平台枚举、账户结构、签名结果等核心数据类型
 */

// ============================================================================
// 核心枚举定义
// ============================================================================

export enum Platform {
  PACIFICA = 'pacifica',
  ASTER = 'aster',
  BINANCE = 'binance',
}

export enum SignatureType {
  ED25519 = 'ed25519',
  ECDSA_ETHEREUM = 'ecdsa-ethereum',
  HMAC_SHA256 = 'hmac-sha256',
}

export enum AccountStatus {
  LOADING = 'loading',
  ACTIVE = 'active',
  ERROR = 'error',
  REMOVED = 'removed',
}

// ============================================================================
// 凭证类型定义
// ============================================================================

export interface PacificaCredentials {
  type: 'pacifica';
  privateKey: string; // 64字符十六进制Ed25519私钥
}

export interface AsterCredentials {
  type: 'aster';
  privateKey: string; // 以太坊格式私钥 (0x前缀)
}

export interface BinanceCredentials {
  type: 'binance';
  apiKey: string;
  secretKey: string;
}

export type Credentials = PacificaCredentials | AsterCredentials | BinanceCredentials;

// ============================================================================
// 核心实体接口
// ============================================================================

export interface Account {
  id: string;
  platform: Platform;
  credentials: Credentials;
  status?: AccountStatus;
  metadata?: {
    alias?: string;
    description?: string;
    [key: string]: any;
  } | undefined;
}

export interface SignResult {
  success: boolean;
  signature?: string;
  algorithm: string;
  timestamp: Date;
  executionTime?: number;
  error?: string;
}

// ============================================================================
// 请求/响应类型
// ============================================================================

export interface LoadResult {
  success: boolean;
  accounts: Account[];
  errors?: string[] | undefined;
  loadTime: number; // 加载耗时（毫秒）
}

export interface ConfigFile {
  version: string;
  accounts: AccountConfig[];
}

export interface AccountConfig {
  id: string;
  platform: Platform;
  credentials: Credentials;
  metadata?: Record<string, any>;
}

export interface SignRequest {
  accountId: string;
  message: Uint8Array;
  options?: {
    timeout?: number;
    [key: string]: any;
  };
}

export interface SignResponse {
  success: boolean;
  signature?: string;
  algorithm: string;
  timestamp: Date;
  error?: string;
}

export interface VerifyRequest {
  accountId: string;
  message: Uint8Array;
  signature: string;
}

export interface VerifyResponse {
  success: boolean;
  algorithm: string;
  timestamp: Date;
  error?: string;
}

// ============================================================================
// 错误类型
// ============================================================================

export enum ErrorType {
  CONFIG_LOAD_ERROR = 'config_load_error',
  VALIDATION_ERROR = 'validation_error',
  SIGNATURE_ERROR = 'signature_error',
  PLATFORM_DETECTION_ERROR = 'platform_detection_error',
  KEY_GENERATION_ERROR = 'key_generation_error',
  KEY_DERIVATION_ERROR = 'key_derivation_error',
  ADDRESS_GENERATION_ERROR = 'address_generation_error',
  SERIALIZATION_ERROR = 'serialization_error',
}

export interface CredentialError {
  type: ErrorType;
  message: string;
  accountId?: string;
  timestamp: Date;
  details?: any;
}

export class CredentialManagerError extends Error {
  constructor(
    message: string,
    public readonly type: ErrorType,
    public readonly details?: any
  ) {
    super(message);
    this.name = 'CredentialManagerError';
  }
}

// ============================================================================
// 工厂选项
// ============================================================================

export interface CredentialManagerOptions {
  /**
   * 是否启用文件监听
   * @default true
   */
  enableFileWatching?: boolean;

  /**
   * 签名超时时间（毫秒）
   * @default 30000
   */
  signTimeout?: number;

  /**
   * 是否启用日志记录
   * @default true
   */
  enableLogging?: boolean;

  /**
   * 日志级别
   * @default 'info'
   */
  logLevel?: 'debug' | 'info' | 'warn' | 'error';
}

// ============================================================================
// 平台检测相关
// ============================================================================

export interface DetectionResult {
  platform: Platform | null;
  confidence: number;
  detectors: Array<{
    detector: string;
    platform: Platform | null;
    confidence: number;
  }>;
}

// ============================================================================
// 管理器统计
// ============================================================================

export interface ManagerStats {
  totalAccounts: number;
  accountsByPlatform: Record<Platform, number>;
  accountsByStatus: Record<AccountStatus, number>;
  totalSignatures: number;
  successRate: number;
  averageSignatureTime: number;
  memoryUsage: number;
  uptime: number;
}