/**
 * 自动化部署测试模块类型定义
 *
 * @author czj
 * @date 2026-08-12
 */

/** 认证类型 */
export type AuthType = 'password' | 'key'

/** 服务器连接状态 */
export type ServerStatus = 'unknown' | 'online' | 'offline'

/** 执行状态 */
export type ExecutionStatus = 'pending' | 'running' | 'completed' | 'failed' | 'cancelled'

/** 日志级别 */
export type LogLevel = 'INFO' | 'WARN' | 'ERROR' | 'INPUT' | 'OUTPUT'

/** 触发匹配方式 */
export type TriggerType = 'contains' | 'regex'

// ==================== 服务器 ====================

/** 目标服务器 */
export interface DeployServer {
  id: string
  name: string
  host: string
  port: number
  username: string
  authType: AuthType
  description: string | null
  status: ServerStatus
  createdAt: string
  updatedAt: string
}

/** 创建服务器请求 */
export interface DeployServerCreate {
  name: string
  host: string
  port?: number
  username: string
  password?: string | null
  authType?: AuthType
  privateKey?: string | null
  description?: string | null
}

/** 更新服务器请求 */
export interface DeployServerUpdate {
  name?: string
  host?: string
  port?: number
  username?: string
  password?: string | null
  authType?: AuthType
  privateKey?: string | null
  description?: string | null
}

/** 服务器列表响应 */
export interface DeployServerListResponse {
  items: DeployServer[]
  total: number
}

/** 连接测试响应 */
export interface TestConnectionResponse {
  success: boolean
  message: string
  serverName: string
  host: string
  port: number
}

// ==================== 交互式应答规则 ====================

/** 交互式应答规则 */
export interface ExpectAnswer {
  trigger: string
  triggerType?: TriggerType
  answer: string
  timeout?: number
  order?: number
  sensitive?: boolean
  description?: string | null
}

// ==================== 脚本配置 ====================

/** 部署脚本配置 */
export interface DeployScript {
  id: string
  name: string
  description: string | null
  serverId: string
  serverName: string
  remoteCommand: string
  workDir: string | null
  timeout: number
  expectAnswers: ExpectAnswer[]
  createdAt: string
  updatedAt: string
}

/** 创建脚本配置请求 */
export interface DeployScriptCreate {
  name: string
  description?: string | null
  serverId: string
  remoteCommand: string
  workDir?: string | null
  timeout?: number
  expectAnswers?: ExpectAnswer[]
}

/** 更新脚本配置请求 */
export interface DeployScriptUpdate {
  name?: string
  description?: string | null
  serverId?: string
  remoteCommand?: string
  workDir?: string | null
  timeout?: number
  expectAnswers?: ExpectAnswer[]
}

/** 脚本列表响应 */
export interface DeployScriptListResponse {
  items: DeployScript[]
  total: number
}

// ==================== 执行记录 ====================

/** 部署执行记录 */
export interface DeployExecution {
  id: string
  scriptId: string
  serverId: string
  scriptName: string
  serverName: string
  serverHost: string
  remoteCommand: string
  status: ExecutionStatus
  startTime: string | null
  endTime: string | null
  duration: number
  exitCode: number | null
  errorMessage: string | null
  triggerBy: string
  createdAt: string
}

/** 创建执行记录请求 */
export interface DeployExecutionCreate {
  scriptId: string
  triggerBy?: string
}

/** 执行列表响应 */
export interface DeployExecutionListResponse {
  items: DeployExecution[]
  total: number
}

// ==================== 日志 ====================

/** 部署执行日志 */
export interface DeployLog {
  id: string
  executionId: string
  timestamp: string
  level: LogLevel
  message: string
  isSensitive: boolean
}

/** 日志列表响应 */
export interface DeployLogListResponse {
  items: DeployLog[]
  total: number
}

// ==================== WebSocket 消息 ====================

/** WebSocket 消息类型 */
export type DeployMessageType =
  | 'deploy_start'
  | 'deploy_output'
  | 'deploy_stderr'
  | 'deploy_answer_sent'
  | 'deploy_complete'
  | 'deploy_error'

/** WebSocket 消息 */
export interface DeployMessage {
  type: DeployMessageType
  data: {
    executionId: string
    message?: string
    error?: string
    exitCode?: number
    duration?: number
    trigger?: string
    answer?: string
    sensitive?: boolean
    timestamp?: number
  }
  timestamp?: string
}