export interface ContentCheck {
  /** URL to fetch full HTML from (follows redirects) */
  url: string;
  /** Patterns that indicate a stuck/broken page (e.g. "Загрузка...") */
  badPatterns: string[];
  /** If true, verify that referenced JS chunks (/_next/static/chunks/) return 200 */
  verifyChunks: boolean;
}

export interface ApiEndpointCheck {
  /** Page that depends on this endpoint (for error messages) */
  page: string;
  /** API endpoint path (e.g. /v1/notifications/settings) */
  endpoint: string;
}

export interface ServiceConfig {
  id: string;
  name: string;
  type: 'http' | 'pm2' | 'docker-pg' | 'docker-redis';
  url?: string;
  expectedStatusCodes?: number[];
  timeoutMs?: number;
  pm2Name?: string;
  containerName?: string;
  critical: boolean;
  autoFix?: 'pm2-restart' | 'clear-next-cache' | 'none';
  autoFixCwd?: string;
  /** Deep content check: detect stuck loading states and broken JS chunks */
  contentCheck?: ContentCheck;
  /** Additional pages to check for broken JS chunks (auth-gated pages where "Загрузка..." is expected) */
  internalPages?: ContentCheck[];
  /** API endpoints that this service depends on (checks for errors) */
  apiEndpoints?: ApiEndpointCheck[];
  /** Base URL of the API these endpoints point to */
  apiBaseUrl?: string;
  /** Auth credentials for authenticated API checks */
  apiAuth?: { phone: string; password: string };
}

export interface CheckResult {
  serviceId: string;
  serviceName: string;
  ok: boolean;
  responseTimeMs: number;
  error?: string;
  details?: string;
  timestamp: Date;
}

export interface ServiceState {
  consecutiveFailures: number;
  consecutiveSuccesses: number;
  lastAlertSentAt: number | null;
  lastStatus: 'ok' | 'down' | 'unknown';
  transitions: { timestamp: number; toStatus: 'ok' | 'down' }[];
  autoFixAttempts: number;
  lastAutoFixAt: number | null;
  incidentId: string | null;
  isFlapping: boolean;
  flappingAlertSent: boolean;
  firstFailureAt: number | null;
}

export interface AutoFixResult {
  attempted: boolean;
  action: string;
  success: boolean;
  output: string;
}

export interface Incident {
  id: string;
  serviceId: string;
  serviceName: string;
  status: 'open' | 'resolved';
  severity: 'critical' | 'warning';
  error: string;
  firstFailureAt: string;
  resolvedAt?: string;
  autoFixAttempts: AutoFixResult[];
  notifications: { sentAt: string; type: string }[];
}

export interface DevTask {
  id: string;
  serviceId: string;
  serviceName: string;
  endpoint: string;
  page: string;
  discoveredAt: string;
  status: 'pending' | 'in_progress' | 'done';
}

export interface DevBacklog {
  tasks: DevTask[];
  lastUpdated: string;
}

export interface WatchdogConfig {
  checkIntervalMs: number;
  deployLockPath: string;
  deployGracePeriodMs: number;
  telegram: {
    botToken: string;
    chatId: string;
  };
  consecutiveFailuresBeforeAlert: number;
  flapWindowMs: number;
  flapThreshold: number;
  cooldownMs: number;
  autoFixMax: number;
  autoFixCooldownMs: number;
  incidentDir: string;
  logDir: string;
  services: ServiceConfig[];
}
