import type {
  Channel,
  NormalizedMessage,
  ProviderErrorCode,
  ResolvedCredentials,
} from '@zyphon/shared-types';

export interface ProviderResult {
  success: boolean;
  provider_reference?: string;
  raw_response?: Record<string, unknown>;
  error?: {
    code: ProviderErrorCode;
    message: string;
    raw?: unknown;
  };
}

export type HealthStatus = 'healthy' | 'degraded' | 'down';

export interface ProviderHealth {
  status: HealthStatus;
  latency_ms: number;
  checked_at: number;
  message?: string;
}

export interface WebhookRequest {
  headers: Record<string, string | string[] | undefined>;
  body: unknown;
  rawBody?: Buffer | string;
  query?: Record<string, string | undefined>;
}

export interface DeliveryEvent {
  message_id: string;
  status: 'delivered' | 'undelivered';
  timestamp: number;
  provider: string;
  raw?: unknown;
}

export type FieldInputType = 'text' | 'secret' | 'select' | 'number' | 'textarea';

export type FieldValidatorKind =
  | 'email'
  | 'host'
  | 'port'
  | 'url'
  | 'phone'
  | 'header'
  | 'digits19'
  | 'num';

export interface FieldSelectOption {
  value: string;
  label: string;
}

export interface FieldSchema {
  key: string;
  label: string;
  type: FieldInputType;
  required?: boolean;
  optional?: boolean;
  placeholder?: string;
  default?: string | number;
  help?: string;
  section?: string;
  sectionNote?: string;
  validator?: FieldValidatorKind;
  options?: FieldSelectOption[];
  /** India TRAI DLT field (sender header, PE ID, template ID, etc.) */
  dlt?: boolean;
}

export interface ProviderCatalogEntry {
  type: string;
  name: string;
  description: string;
  channel: Channel | 'push' | 'chat';
  dlt?: boolean;
  help?: string;
  /** Markets where this provider is offered in Connect. Default: global only. */
  markets?: Array<'india' | 'global'>;
  fieldSchema: FieldSchema[];
  max_rps?: number;
  cost_per_message?: number;
}

export interface ProviderAdapter {
  readonly id: string;
  readonly channel: Channel;
  /** Dashboard form schema; optional for mock/test adapters */
  readonly fieldSchema?: FieldSchema[];
  validateConfiguration(c: ResolvedCredentials): Promise<void>;
  send(m: NormalizedMessage, c: ResolvedCredentials, signal: AbortSignal): Promise<ProviderResult>;
  healthCheck(c: ResolvedCredentials): Promise<ProviderHealth>;
  parseWebhook?(req: WebhookRequest): DeliveryEvent | null;
}

export type CircuitBreakerState = 'CLOSED' | 'OPEN' | 'HALF_OPEN';

export interface ProviderRuntimeState {
  circuit_state: CircuitBreakerState;
  consecutive_failures: number;
  open_until?: number;
  total_ok: number;
  total_failed: number;
  last_latency_ms: number;
  recent_latencies: number[];
  recent_results: boolean[]; // sliding window
  health: ProviderHealth;
}

export interface ProviderDefinition {
  id: string;
  channel: Channel;
  adapter: string;
  /** Display name from providers.yaml — kept in registry for list/edit UIs. */
  name?: string;
  enabled: boolean;
  latency_ms?: number;
  failure_rate?: number;
  max_rps?: number;
  cost_per_message?: number;
  config?: Record<string, unknown>;
}

