/**
 * Preview Deployment Types
 *
 * Configuration and status types for Docker-based preview deployments.
 * Docker IS the source of truth — no database table needed.
 */

/**
 * Dev mode configuration for a service.
 * Used when running from an agent's worktree with hot-reload.
 */
export interface PreviewServiceDevConfig {
  image: string;
  command?: string;
  workdir?: string;
}

/**
 * Service configuration within a preview deployment.
 */
export interface PreviewServiceConfig {
  name: string;
  build?: { context: string; dockerfile: string } | string;
  image?: string;
  port: number;
  route?: string;
  internal?: boolean;
  healthcheck?: { path: string; interval?: string; retries?: number };
  env?: Record<string, string>;
  volumes?: string[];
  dev?: PreviewServiceDevConfig;
  seed?: string[];
}

/**
 * Preview deployment configuration.
 * Parsed from `.cw-preview.yml`, existing compose file, or inferred from Dockerfile.
 */
export interface PreviewConfig {
  version: 1;
  compose?: string;
  services: Record<string, PreviewServiceConfig>;
}

/**
 * Runtime status of a preview deployment.
 * Reconstructed from Docker state + container labels.
 */
export interface PreviewStatus {
  id: string;
  projectName: string;
  initiativeId: string;
  phaseId?: string;
  projectId: string;
  branch: string;
  gatewayPort: number;
  url: string;
  mode: 'preview' | 'dev';
  status: 'building' | 'running' | 'stopped' | 'failed';
  services: Array<{ name: string; state: string; health: string }>;
  composePath: string;
}

/**
 * Docker labels applied to preview containers for metadata retrieval.
 */
export const PREVIEW_LABEL_PREFIX = 'cw';
export const PREVIEW_LABELS = {
  preview: `${PREVIEW_LABEL_PREFIX}.preview`,
  initiativeId: `${PREVIEW_LABEL_PREFIX}.initiative-id`,
  phaseId: `${PREVIEW_LABEL_PREFIX}.phase-id`,
  branch: `${PREVIEW_LABEL_PREFIX}.branch`,
  projectId: `${PREVIEW_LABEL_PREFIX}.project-id`,
  port: `${PREVIEW_LABEL_PREFIX}.port`,
  previewId: `${PREVIEW_LABEL_PREFIX}.preview-id`,
  mode: `${PREVIEW_LABEL_PREFIX}.mode`,
  agentId: `${PREVIEW_LABEL_PREFIX}.agent-id`,
} as const;

/**
 * Compose project name prefix for all preview deployments.
 */
export const COMPOSE_PROJECT_PREFIX = 'cw-preview-';

/**
 * Gateway compose project name and shared Docker network.
 */
export const GATEWAY_PROJECT_NAME = 'cw-preview-gateway';
export const GATEWAY_NETWORK = 'cw-preview-net';

/**
 * Options for starting a preview deployment.
 */
export interface StartPreviewOptions {
  initiativeId: string;
  phaseId?: string;
  projectId: string;
  branch: string;
  mode?: 'preview' | 'dev';
  worktreePath?: string;
  agentId?: string;
}

/**
 * Health check result for a single service.
 */
export interface HealthResult {
  name: string;
  healthy: boolean;
  error?: string;
}
