export interface Instance {
  id: string;
  slug: string;
  name: string;
  domain: string;
  status: 'PROVISIONING' | 'RUNNING' | 'STOPPED' | 'ERROR' | 'DESTROYING';
  statusMessage?: string;
  basePath: string;
  composeProject: string;
  gitBranch: string;
  gitCommit?: string;
  imageTag?: string;
  portConfig: Record<string, number>;
  enableMedia: boolean;
  enableChat: boolean;
  enableGancio: boolean;
  enableListmonk: boolean;
  enableMonitoring: boolean;
  enableDevTools: boolean;
  enablePayments: boolean;
  enableMeet: boolean;
  enableSms: boolean;
  enableSocial: boolean;
  enablePeople: boolean;
  enableAnalytics: boolean;
  jvbAdvertiseIp?: string;
  isRegistered: boolean;
  isRemote: boolean;
  agentUrl?: string;
  agentFingerprint?: string;
  agentVersion?: string;
  agentLastSeen?: string;
  adminEmail: string;
  pangolinEndpoint?: string;
  pangolinSiteId?: string;
  pangolinNewtId?: string;
  smtpHost?: string;
  smtpPort?: number;
  smtpUser?: string;
  smtpFrom?: string;
  emailTestMode: boolean;
  notes?: string;
  createdAt: string;
  updatedAt: string;
  lastHealthCheck?: string;
  portAllocations?: PortAllocation[];
  healthChecks?: HealthCheck[];
  backups?: Backup[];
  _count?: { healthChecks: number; backups: number };
}

export interface PortAllocation {
  id: string;
  port: number;
  instanceId: string;
  service: string;
  notes?: string;
}

export interface HealthCheck {
  id: string;
  instanceId: string;
  status: 'HEALTHY' | 'DEGRADED' | 'UNHEALTHY' | 'UNKNOWN';
  serviceStatus: Record<string, unknown>;
  totalServices: number;
  healthyServices: number;
  responseTimeMs?: number;
  checkedAt: string;
}

export interface Backup {
  id: string;
  instanceId: string;
  status: 'PENDING' | 'IN_PROGRESS' | 'COMPLETED' | 'FAILED';
  archivePath?: string;
  sizeBytes?: number;
  manifest?: Record<string, unknown>;
  startedAt: string;
  completedAt?: string;
  errorMessage?: string;
  s3Uploaded: boolean;
}

// ─── Discovery Types ──────────────────────────────────────────────

export interface DiscoveredInstance {
  name: string;
  slug: string;
  domain: string;
  basePath: string;
  composeProject: string;
  portConfig: { api: number; admin: number; postgres: number; nginx: number };
  adminEmail: string;
  enableMedia: boolean;
  enableChat: boolean;
  enableGancio: boolean;
  enableListmonk: boolean;
  enableMonitoring: boolean;
  enableDevTools: boolean;
  enablePayments: boolean;
  enableMeet: boolean;
  enableSms: boolean;
  enableSocial: boolean;
  enablePeople: boolean;
  enableAnalytics: boolean;
  emailTestMode: boolean;
  source: 'parent' | 'docker';
  isRunning: boolean;
  runningContainers: number;
  totalContainers: number;
  isAlreadyRegistered: boolean;
  existingInstanceId?: string;
  isParentInstance: boolean;
}

export interface DiscoverySummary {
  total: number;
  newInstances: number;
  alreadyRegistered: number;
  running: number;
  parentFound: boolean;
}

export interface DiscoveryResult {
  instances: DiscoveredInstance[];
  summary: DiscoverySummary;
}

export interface ImportResultItem {
  slug: string;
  success: boolean;
  instanceId?: string;
  error?: string;
}

export interface ImportResult {
  results: ImportResultItem[];
  summary: { total: number; succeeded: number; failed: number };
}

// ─── Upgrade Types ───────────────────────────────────────────────

export interface UpdateStatus {
  branch: string;
  currentCommit: string;
  currentMessage?: string;
  remoteCommit: string | null;
  commitsBehind: number;
  changelog: Array<{ hash: string; message: string; date: string; author: string }>;
  checkedAt: string;
  error: string | null;
}

export interface InstanceUpgrade {
  id: string;
  instanceId: string;
  status: 'PENDING' | 'IN_PROGRESS' | 'COMPLETED' | 'FAILED' | 'ROLLED_BACK';
  previousCommit?: string;
  newCommit?: string;
  commitCount: number;
  branch: string;
  currentPhase: number;
  phaseName?: string;
  percentage: number;
  progressMessage?: string;
  durationSeconds?: number;
  errorMessage?: string;
  warnings?: string[];
  log?: string;
  startedAt: string;
  completedAt?: string;
  triggeredBy?: { id: string; name: string; email: string };
}

// ─── Event Types ─────────────────────────────────────────────────

export interface InstanceEvent {
  id: string;
  instanceId: string;
  severity: 'ERROR' | 'WARNING' | 'INFO';
  source: string;
  title: string;
  message: string;
  metadata?: Record<string, unknown>;
  acknowledged: boolean;
  acknowledgedAt?: string;
  acknowledgedBy?: { id: string; name: string; email: string };
  createdAt: string;
  instance?: { id: string; name: string; slug: string };
}

export interface EventSummary {
  errors: number;
  warnings: number;
  infos: number;
  total: number;
  recentErrors: InstanceEvent[];
}

// ─── Audit Types ──────────────────────────────────────────────────

export interface AuditLogEntry {
  id: string;
  userId?: string;
  instanceId?: string;
  action: string;
  details?: Record<string, unknown>;
  ipAddress?: string;
  createdAt: string;
  user?: { id: string; email: string; name: string } | null;
  instance?: { id: string; name: string; slug: string } | null;
}

// ─── Remote Agent Types ─────────────────────────────────────────────

export interface AgentRegistration {
  id: string;
  inviteCodeId: string;
  slug: string;
  name: string;
  domain: string;
  agentUrl: string;
  basePath: string;
  composeProject: string;
  metadata?: Record<string, unknown>;
  status: 'PENDING' | 'APPROVED' | 'REJECTED' | 'EXPIRED';
  instanceId?: string;
  approvedById?: string;
  approvedAt?: string;
  rejectedAt?: string;
  createdAt: string;
}

export interface AgentInviteCode {
  id: string;
  code: string;
  createdById: string;
  usedById?: string;
  expiresAt: string;
  usedAt?: string;
  createdAt: string;
  createdBy?: { id: string; name: string; email: string };
}

export interface AgentStatus {
  reachable: boolean;
  version?: string;
  uptime?: number;
  error?: string;
}
