export type ServerCommandKey = 'test' | 'rel' | 'prod' | 'work-server' | 'command-runner';

export type RestartReservationWorkloadSummary = {
  codexRunningCount: number;
  codexQueuedCount: number;
  automationRunningCount: number;
  automationQueuedCount: number;
};

export type ServerCommandItem = {
  key: ServerCommandKey;
  label: string;
  summary: string;
  environment: string;
  publicUrl: string | null;
  checkUrl: string;
  composeFile: string;
  serviceName: string;
  availability: 'online' | 'degraded' | 'offline';
  httpStatus: number | null;
  contentType: string | null;
  responsePreview: string | null;
  checkedAt: string;
  startedAt: string | null;
  runningVersion: string | null;
  runningBuiltAt: string | null;
  latestVersion: string | null;
  latestBuiltAt: string | null;
  latestSourceChangeAt: string | null;
  latestSourceChangePath: string | null;
  buildRequired: boolean;
  updateAvailable: boolean;
  updateSummary: string | null;
  responseTimeMs: number | null;
  composeStatus: string | null;
  composeDetails: string | null;
  lastCommand: string;
  commandScript: string;
  commandWorkingDirectory: string;
  errorMessage: string | null;
  deployment: WorkServerDeploymentState | null;
};

export type ServerCommandActionResult = {
  item: ServerCommandItem;
  commandOutput: string | null;
  restartState: 'completed' | 'accepted';
  deployment?: WorkServerDeploymentState | null;
  testDeployment?: TestServerDeploymentState | null;
};

export type WorkServerDeploymentStepKey =
  | 'build-target-slot'
  | 'verify-target-health'
  | 'switch-proxy'
  | 'drain-previous-slot'
  | 'rebuild-previous-slot'
  | 'recover-interrupted-chat';

export type WorkServerDeploymentStepStatus = 'pending' | 'running' | 'completed' | 'failed';

export type WorkServerDeploymentStep = {
  key: WorkServerDeploymentStepKey;
  status: WorkServerDeploymentStepStatus;
  detail: string | null;
  updatedAt: string | null;
};

export type WorkServerDeploymentPhase =
  | 'idle'
  | 'build-target-slot'
  | 'verify-target-health'
  | 'switch-proxy'
  | 'drain-previous-slot'
  | 'rebuild-previous-slot'
  | 'recover-interrupted-chat'
  | 'completed'
  | 'failed';

export type WorkServerDeploymentState = {
  status: 'idle' | 'running' | 'completed' | 'failed';
  phase: WorkServerDeploymentPhase;
  summary: string | null;
  startedAt: string | null;
  updatedAt: string | null;
  completedAt: string | null;
  activeSlot: 'blue' | 'green' | null;
  targetSlot: 'blue' | 'green' | null;
  previousSlot: 'blue' | 'green' | null;
  targetContainer: string | null;
  previousContainer: string | null;
  previousSlotActiveChatRequestCount: number | null;
  previousSlotQueuedChatRequestCount: number | null;
  recoveredSessionCount: number | null;
  recoveredRestartedCount: number | null;
  recoveredRequeuedCount: number | null;
  lastError: string | null;
  logExcerpt: string | null;
  steps: WorkServerDeploymentStep[];
};

export type TestServerDeploymentStepKey = 'commit-main-worktree' | 'push-origin-main' | 'build-test-app' | 'deploy-test-server';

export type TestServerDeploymentStepStatus = 'pending' | 'running' | 'completed' | 'failed';

export type TestServerDeploymentStep = {
  key: TestServerDeploymentStepKey;
  status: TestServerDeploymentStepStatus;
  detail: string | null;
  updatedAt: string | null;
};

export type TestServerDeploymentPhase =
  | 'idle'
  | 'commit-main-worktree'
  | 'push-origin-main'
  | 'build-test-app'
  | 'deploy-test-server'
  | 'completed'
  | 'failed';

export type TestServerDeploymentState = {
  status: 'idle' | 'running' | 'completed' | 'failed';
  phase: TestServerDeploymentPhase;
  summary: string | null;
  startedAt: string | null;
  updatedAt: string | null;
  completedAt: string | null;
  lastError: string | null;
  logExcerpt: string | null;
  steps: TestServerDeploymentStep[];
};

export type ServerRestartReservationStatus =
  | 'idle'
  | 'waiting'
  | 'ready'
  | 'executing'
  | 'recovering'
  | 'completed'
  | 'cancelled'
  | 'failed';

export type ServerRestartReservationExecutionPhase =
  | 'idle'
  | 'commit-main-worktree'
  | 'restart-test'
  | 'restart-work-server'
  | 'verify-runtime';

export type ServerRestartReservationWorkItem = {
  kind: 'codex' | 'automation';
  status: 'running' | 'queued' | 'waiting';
  title: string;
  detail: string | null;
  requestId: string | null;
  sessionId: string | null;
};

export type ServerRestartReservationAutoFix = {
  enabled: boolean;
  targetKey: 'test' | 'work-server' | null;
  requestId: string | null;
  sessionId: string | null;
  status: 'idle' | 'queued' | 'running' | 'completed' | 'failed';
  summary: string | null;
  detail: string | null;
  requestedAt: string | null;
  startedAt: string | null;
  completedAt: string | null;
  failedAt: string | null;
};

export type ServerRestartReservation = {
  enabled: boolean;
  target: 'all' | 'test' | 'work-server';
  status: ServerRestartReservationStatus;
  requestedAt: string | null;
  requestedByClientId: string | null;
  lastCheckedAt: string | null;
  nextCheckAt: string | null;
  waitingReason: string | null;
  workloadSummary: RestartReservationWorkloadSummary;
  startedAt: string | null;
  completedAt: string | null;
  cancelledAt: string | null;
  lastError: string | null;
  activeClientCount: number;
  notifiedActiveClientsAt: string | null;
  appOrigin: string | null;
  autoExecuteAt: string | null;
  autoExecuteDelaySeconds: number;
  executionPhase: ServerRestartReservationExecutionPhase;
  updatedAt: string | null;
  workItems: ServerRestartReservationWorkItem[];
  autoFix: ServerRestartReservationAutoFix;
};
