/**
 * GDPR-Compliant Data Retention Policies
 *
 * Defines retention periods for different data types in the analytics system.
 * All periods are configurable via environment variables with sensible defaults.
 *
 * Compliance Notes:
 * - Raw event data: Limited retention (GDPR principle of storage limitation)
 * - Aggregated metrics: Longer retention (legitimate business interest)
 * - Revenue data: 7 years (tax/legal requirement, anonymized after primary period)
 */

export interface RetentionPolicy {
  /** Human-readable description of the policy */
  description: string;
  /** Retention period in days */
  retentionDays: number;
  /** Whether to anonymize instead of delete (for legal requirements) */
  anonymize: boolean;
  /** Database table name */
  tableName: string;
  /** Column name for timestamp filtering */
  timestampColumn: string;
  /** Column name for user/subject ID filtering (for GDPR deletion) */
  subjectColumn?: string;
}

export interface RetentionPolicies {
  /** Aggregated metrics data */
  aggregatedMetrics: RetentionPolicy;
  /** Session/event data (if using collector) */
  sessions?: RetentionPolicy;
  /** Raw events (if using collector) */
  rawEvents?: RetentionPolicy;
  /** Revenue/financial data */
  revenue?: RetentionPolicy;
  /** Custom policies for extensions */
  [key: string]: RetentionPolicy | undefined;
}

/**
 * Get retention policies from environment or use defaults.
 *
 * Environment variables:
 * - RETENTION_RAW_EVENTS_DAYS: Raw event data retention (default: 90)
 * - RETENTION_AGGREGATED_DAYS: Aggregated metrics retention (default: 730 = 2 years)
 * - RETENTION_REVENUE_DAYS: Revenue data retention (default: 2555 = 7 years)
 */
export function getRetentionPolicies(): RetentionPolicies {
  const rawEventsDays = parseInt(
    process.env.RETENTION_RAW_EVENTS_DAYS || '90',
    10,
  );
  const aggregatedDays = parseInt(
    process.env.RETENTION_AGGREGATED_DAYS || '730',
    10,
  );
  const revenueDays = parseInt(
    process.env.RETENTION_REVENUE_DAYS || '2555',
    10,
  );

  return {
    // Aggregated metrics - 2 years (business analytics)
    aggregatedMetrics: {
      description: 'Aggregated analytics metrics',
      retentionDays: aggregatedDays,
      anonymize: false,
      tableName: 'aggregated_metrics',
      timestampColumn: 'timestamp',
    },

    // Raw events - 90 days (GDPR storage limitation)
    sessions: {
      description: 'Session tracking data',
      retentionDays: rawEventsDays,
      anonymize: false,
      tableName: 'sessions',
      timestampColumn: 'created_at',
      subjectColumn: 'subject_id',
    },

    rawEvents: {
      description: 'Raw tracking events',
      retentionDays: rawEventsDays,
      anonymize: false,
      tableName: 'events',
      timestampColumn: 'timestamp',
      subjectColumn: 'subject_id',
    },

    // Revenue data - 7 years (tax/legal requirement, anonymize after 90 days)
    revenue: {
      description: 'Revenue transaction records (tax requirement)',
      retentionDays: revenueDays,
      anonymize: true,
      tableName: 'revenue_metrics',
      timestampColumn: 'timestamp',
      subjectColumn: 'subject_id',
    },
  };
}

/**
 * Get anonymization threshold for revenue data.
 *
 * Revenue records older than this threshold should have subject_id anonymized
 * while retaining the record for tax/legal compliance.
 */
export function getRevenueAnonymizationThresholdDays(): number {
  return parseInt(
    process.env.RETENTION_REVENUE_ANONYMIZE_AFTER_DAYS || '90',
    10,
  );
}

/**
 * Standard retention periods for reference
 */
export const RETENTION_PERIODS = {
  /** Raw events - GDPR storage limitation principle */
  RAW_EVENTS: 90,
  /** Aggregated analytics - legitimate business interest */
  AGGREGATED: 730,
  /** Revenue data - tax compliance requirement */
  REVENUE: 2555,
  /** Security audit logs - security requirement */
  SECURITY_LOGS: 1095,
  /** Error logs - operational need */
  ERROR_LOGS: 180,
} as const;
