/**
 * Data Retention Policies module — types and schemas.
 * Manages per-product, per-container retention rules, scheduled purge jobs,
 * and audit trail of purged data.
 */

import { z } from 'zod';

// ── Retention Types ──────────────────────────────────────────────

export type RetentionAction = 'delete' | 'archive' | 'anonymize';
export type RetentionJobStatus = 'pending' | 'running' | 'completed' | 'failed';

export interface RetentionPolicyDoc {
  id: string;
  productId: string;
  /** Target Cosmos container name */
  containerName: string;
  /** Human-readable label */
  label: string;
  /** Retention period in days (e.g. 90 = delete docs older than 90 days) */
  retentionDays: number;
  /** What to do when retention expires */
  action: RetentionAction;
  /** Which date field to evaluate (e.g. "createdAt", "updatedAt") */
  dateField: string;
  /** Optional additional filter (e.g. "status = 'completed'") */
  filter: string | null;
  /** Whether the policy is actively enforced */
  isEnabled: boolean;
  /** Cron-like schedule description for when purge runs */
  schedule: string;
  /** Stats from last run */
  lastRunAt: string | null;
  lastRunPurgedCount: number;
  createdAt: string;
  updatedAt: string;
  updatedBy: string;
}

export interface RetentionJobDoc {
  id: string;
  productId: string;
  policyId: string;
  containerName: string;
  status: RetentionJobStatus;
  /** Number of documents evaluated */
  scannedCount: number;
  /** Number of documents purged */
  purgedCount: number;
  /** Number of documents that failed */
  errorCount: number;
  /** Error message if job failed */
  error: string | null;
  startedAt: string;
  completedAt: string | null;
}

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

export const CreateRetentionPolicySchema = z.object({
  containerName: z.string().min(1).max(128),
  label: z.string().min(1).max(200),
  retentionDays: z.number().int().min(1).max(3650), // up to 10 years
  action: z.enum(['delete', 'archive', 'anonymize']).default('delete'),
  dateField: z.string().min(1).max(64).default('createdAt'),
  filter: z.string().max(500).nullable().default(null),
  isEnabled: z.boolean().default(true),
  schedule: z.string().min(1).max(100).default('daily'),
});

export const UpdateRetentionPolicySchema = z.object({
  label: z.string().min(1).max(200).optional(),
  retentionDays: z.number().int().min(1).max(3650).optional(),
  action: z.enum(['delete', 'archive', 'anonymize']).optional(),
  dateField: z.string().min(1).max(64).optional(),
  filter: z.string().max(500).nullable().optional(),
  isEnabled: z.boolean().optional(),
  schedule: z.string().min(1).max(100).optional(),
});

export type CreateRetentionPolicyInput = z.infer<typeof CreateRetentionPolicySchema>;
export type UpdateRetentionPolicyInput = z.infer<typeof UpdateRetentionPolicySchema>;

// ── Helpers ──────────────────────────────────────────────────────

export function getCutoffDate(retentionDays: number): string {
  const cutoff = new Date();
  cutoff.setDate(cutoff.getDate() - retentionDays);
  return cutoff.toISOString();
}
