/**
 * Backup/Restore module — types and schemas.
 * Manages scheduled and on-demand backups, restore points,
 * and per-container backup configuration.
 */

import { z } from 'zod';

// ── Backup Types ──────────────────────────────────────────────────

export type BackupStatus = 'pending' | 'running' | 'completed' | 'failed';
export type BackupType = 'full' | 'incremental' | 'selective';
export type RestoreStatus = 'pending' | 'running' | 'completed' | 'failed' | 'rolled_back';

export interface BackupDoc {
  id: string;
  productId: string;
  /** Human-readable label */
  label: string;
  type: BackupType;
  status: BackupStatus;
  /** Containers included in this backup */
  containers: string[];
  /** Total documents backed up */
  documentCount: number;
  /** Backup size in bytes (estimated) */
  sizeBytes: number;
  /** Blob storage path for the backup archive */
  storagePath: string | null;
  /** Error message if failed */
  error: string | null;
  createdBy: string;
  startedAt: string;
  completedAt: string | null;
  /** Expiry date for auto-cleanup */
  expiresAt: string | null;
}

export interface RestoreDoc {
  id: string;
  productId: string;
  backupId: string;
  status: RestoreStatus;
  /** Containers restored */
  containers: string[];
  /** Documents restored */
  documentCount: number;
  /** Error message if failed */
  error: string | null;
  requestedBy: string;
  startedAt: string;
  completedAt: string | null;
}

export interface BackupConfigDoc {
  id: string;
  productId: string;
  /** Containers to include in scheduled backups */
  includedContainers: string[];
  /** Containers to exclude from backups */
  excludedContainers: string[];
  /** Whether scheduled backups are enabled */
  scheduledBackupsEnabled: boolean;
  /** Schedule description (e.g. "daily", "weekly") */
  schedule: string;
  /** Backup retention in days */
  retentionDays: number;
  /** Max number of backups to retain */
  maxBackups: number;
  updatedAt: string;
  updatedBy: string;
}

// ── Schemas ──────────────────────────────────────────────────────

export const CreateBackupSchema = z.object({
  label: z.string().min(1).max(200),
  type: z.enum(['full', 'incremental', 'selective']).default('full'),
  containers: z.array(z.string().min(1).max(128)).max(50).optional(),
  expiresInDays: z.number().int().min(1).max(365).optional(),
});

export const RestoreBackupSchema = z.object({
  backupId: z.string().min(1),
  containers: z.array(z.string().min(1).max(128)).max(50).optional(),
});

export const UpdateBackupConfigSchema = z.object({
  includedContainers: z.array(z.string().min(1).max(128)).max(100).optional(),
  excludedContainers: z.array(z.string().min(1).max(128)).max(100).optional(),
  scheduledBackupsEnabled: z.boolean().optional(),
  schedule: z.string().min(1).max(100).optional(),
  retentionDays: z.number().int().min(1).max(365).optional(),
  maxBackups: z.number().int().min(1).max(100).optional(),
});

export type CreateBackupInput = z.infer<typeof CreateBackupSchema>;
export type RestoreBackupInput = z.infer<typeof RestoreBackupSchema>;
export type UpdateBackupConfigInput = z.infer<typeof UpdateBackupConfigSchema>;
