// Generated TypeScript types from Go contracts
// DO NOT EDIT - This file is auto-generated by cmd/generate-types

export interface APIResponse<T = any> {
  success: boolean;
  data?: T;
  error?: string;
}

// Health constants - generated from internal/health/constants.go
// Health levels
export const HealthLevelHealthy = 'healthy' as const;
export const HealthLevelDegraded = 'degraded' as const;
export const HealthLevelUnhealthy = 'unhealthy' as const;
export type HealthLevel = typeof HealthLevelHealthy | typeof HealthLevelDegraded | typeof HealthLevelUnhealthy;

// Admin states
export const AdminStateEnabled = 'enabled' as const;
export const AdminStateDisabled = 'disabled' as const;
export const AdminStateQuarantined = 'quarantined' as const;
export type AdminState = typeof AdminStateEnabled | typeof AdminStateDisabled | typeof AdminStateQuarantined;

// Health actions - suggested remediation for health issues
export const HealthActionNone = '' as const;
export const HealthActionLogin = 'login' as const;
export const HealthActionRestart = 'restart' as const;
export const HealthActionEnable = 'enable' as const;
export const HealthActionApprove = 'approve' as const;
export const HealthActionViewLogs = 'view_logs' as const;
export const HealthActionSetSecret = 'set_secret' as const;
export const HealthActionConfigure = 'configure' as const;
export type HealthAction =
  | typeof HealthActionNone
  | typeof HealthActionLogin
  | typeof HealthActionRestart
  | typeof HealthActionEnable
  | typeof HealthActionApprove
  | typeof HealthActionViewLogs
  | typeof HealthActionSetSecret
  | typeof HealthActionConfigure;

export interface HealthStatus {
  level: HealthLevel;
  admin_state: AdminState;
  summary: string;
  detail?: string;
  action?: HealthAction;
}

export interface Server {
  id: string;
  name: string;
  url?: string;
  protocol: string;
  command?: string;
  args?: string[];
  working_dir?: string;
  env?: Record<string, string>;
  headers?: Record<string, string>;
  oauth?: OAuthConfig;
  enabled: boolean;
  quarantined: boolean;
  connected: boolean;
  connecting?: boolean; // Connection attempt in progress
  authenticated?: boolean; // Has stored OAuth token (regardless of validity)
  status: string;
  last_error?: string;
  connected_at?: string; // ISO date string
  last_reconnect_at?: string; // ISO date string
  reconnect_count: number;
  tool_count: number;
  tool_list_token_size?: number; // Token size for this server's tools
  created: string; // ISO date string
  updated: string; // ISO date string
  isolation?: IsolationConfig;
  isolation_defaults?: IsolationDefaults; // Resolved baseline values (read-only, used as placeholders)
  oauth_status?: 'authenticated' | 'expired' | 'error' | 'none'; // OAuth authentication status
  token_expires_at?: string; // ISO date string when OAuth token expires
  user_logged_out?: boolean; // True if user explicitly logged out (prevents auto-reconnection)
  health?: HealthStatus; // Unified health status calculated by the backend
}

export interface OAuthConfig {
  auth_url?: string;
  token_url?: string;
  client_id: string;
  scopes?: string[];
  extra_params?: Record<string, string>;
  redirect_port?: number;
}

export interface IsolationConfig {
  enabled: boolean;
  image?: string;
  network_mode?: string;
  extra_args?: string[];
  memory_limit?: string;
  cpu_limit?: string;
  working_dir?: string;
  timeout?: string;
}

// IsolationDefaults reports the resolved baseline Docker isolation
// values the backend will apply when no per-server override is set.
// Populated on server-list / server-get responses; the Web UI uses these
// as placeholders so "empty = inherit" is discoverable instead of
// mysterious. Never sent back on PATCH requests.
export interface IsolationDefaults {
  runtime_type?: string;
  image?: string;
  network_mode?: string;
  extra_args?: string[];
  working_dir?: string;
}

export interface Tool {
  name: string;
  server_name: string;
  description: string;
  schema?: Record<string, any>;
  usage: number;
  last_used?: string; // ISO date string
  // Mirrors contracts.Tool.Disabled on the Go side — present when an
  // approval record exists for this tool. Absent means "enabled" (default).
  disabled?: boolean;
  // Tool-level quarantine status surfaced by the same approval record.
  // Optional because non-quarantined tools simply omit the field.
  approval_status?: string;
}

export interface SearchResult {
  tool: Tool;
  score: number;
  snippet?: string;
  matches: number;
}

export interface ServerStats {
  total_servers: number;
  connected_servers: number;
  quarantined_servers: number;
  total_tools: number;
  docker_containers: number;
}

export interface LogEntry {
  timestamp: string; // ISO date string
  level: string;
  message: string;
  server?: string;
  fields?: Record<string, any>;
}

export interface SystemStatus {
  phase: string;
  message: string;
  uptime: number; // duration in nanoseconds
  started_at: string; // ISO date string
  config_path: string;
  log_dir: string;
  runtime: RuntimeStatus;
  servers: ServerStats;
}

export interface RuntimeStatus {
  version: string;
  go_version: string;
  build_time?: string;
  index_status: string;
  storage_status: string;
  last_config_load: string; // ISO date string
}

export interface ToolCallRequest {
  tool_name: string;
  args: Record<string, any>;
}

export interface ToolCallResponse {
  tool_name: string;
  server_name: string;
  result: any;
  error?: string;
  duration: string;
  timestamp: string; // ISO date string
}

export interface Event {
  type: string;
  data: any;
  server?: string;
  timestamp: string; // ISO date string
  metadata?: Record<string, any>;
}

// Secret Management Types
export interface Ref {
  type: string;      // env, keyring, op, age
  name: string;      // environment variable name, keyring alias, etc.
  original: string;  // original reference string
}

export interface EnvVarStatus {
  secret_ref: Ref;
  is_set: boolean;
}

export interface ConfigSecretsResponse {
  secrets: Ref[];
  environment_vars: EnvVarStatus[];
  total_secrets: number;
  total_env_vars: number;
}

export interface MigrationCandidate {
  field: string;      // Field path in configuration
  value: string;      // Masked value for display
  suggested: string;  // Suggested secret reference
  confidence: number; // Confidence score (0.0 to 1.0)
  migrating?: boolean; // UI state for migration in progress
}

export interface MigrationAnalysis {
  candidates: MigrationCandidate[];
  total_found: number;
}

// API Response DTOs
export interface GetServersResponse {
  servers: Server[];
  stats: ServerStats;
}

export interface GetServerToolsResponse {
  server_name: string;
  tools: Tool[];
  count: number;
}

export interface SearchToolsResponse {
  query: string;
  results: SearchResult[];
  total: number;
  took: string;
}

export interface GetServerLogsResponse {
  server_name: string;
  logs: LogEntry[];
  count: number;
}

export interface ServerActionResponse {
  server: string;
  action: string;
  success: boolean;
  async?: boolean;
}

export interface QuarantinedServersResponse {
  servers: Server[];
  count: number;
}

// API Client Helper Types
export type ServerAction = 'enable' | 'disable' | 'restart' | 'login';

export interface ServerToggleRequest {
  server: string;
  action: ServerAction;
}

export interface SearchRequest {
  query: string;
  limit?: number;
}

export interface LogsRequest {
  server: string;
  tail?: number;
}

// SSE Event Types
export type SSEEventType =
  | 'status'
  | 'servers.changed'
  | 'server.connected'
  | 'server.disconnected'
  | 'config.reloaded'
  | 'tools.indexed';

export interface SSEEvent {
  type: SSEEventType;
  data: any;
  timestamp: number; // Unix timestamp
}

// Error types for better error handling
export interface APIError {
  success: false;
  error: string;
}

export interface APISuccess<T> {
  success: true;
  data: T;
}

export type APIResult<T> = APISuccess<T> | APIError;

// Type guards
export function isAPIError(response: APIResponse): response is APIError {
  return !response.success;
}

export function isAPISuccess<T>(response: APIResponse<T>): response is APISuccess<T> {
  return response.success;
}

// Update check types (from internal/updatecheck/types.go)
export interface UpdateInfo {
  available: boolean;
  latest_version?: string;
  release_url?: string;
  checked_at?: string; // ISO date string
  is_prerelease?: boolean;
  check_error?: string;
}

export interface InfoResponse {
  version: string;
  web_ui_url: string;
  listen_addr: string;
  endpoints: {
    http: string;
    socket: string;
  };
  update?: UpdateInfo;
}
