/**
 * DataExport Entity
 * GDPR/reporting data export request management
 * Compatible with erp-core data-export.entity
 *
 * @module Audit
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  Index,
} from 'typeorm';

export type ExportType = 'report' | 'backup' | 'gdpr_request' | 'bulk_export';
export type ExportFormat = 'csv' | 'xlsx' | 'pdf' | 'json';
export type ExportStatus = 'pending' | 'processing' | 'completed' | 'failed' | 'expired';

@Entity({ name: 'data_exports', schema: 'audit' })
export class DataExport {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Index()
  @Column({ name: 'tenant_id', type: 'uuid' })
  tenantId: string;

  @Index()
  @Column({ name: 'user_id', type: 'uuid' })
  userId: string;

  @Column({ name: 'export_type', type: 'varchar', length: 50 })
  exportType: ExportType;

  @Column({ name: 'export_format', type: 'varchar', length: 20, nullable: true })
  exportFormat: ExportFormat;

  @Column({ name: 'entity_types', type: 'text', array: true })
  entityTypes: string[];

  @Column({ name: 'filters', type: 'jsonb', default: {} })
  filters: Record<string, any>;

  @Column({ name: 'date_range_start', type: 'timestamptz', nullable: true })
  dateRangeStart: Date;

  @Column({ name: 'date_range_end', type: 'timestamptz', nullable: true })
  dateRangeEnd: Date;

  @Column({ name: 'record_count', type: 'int', nullable: true })
  recordCount: number;

  @Column({ name: 'file_size_bytes', type: 'bigint', nullable: true })
  fileSizeBytes: number;

  @Column({ name: 'file_hash', type: 'varchar', length: 64, nullable: true })
  fileHash: string;

  @Index()
  @Column({ name: 'status', type: 'varchar', length: 20, default: 'pending' })
  status: ExportStatus;

  @Column({ name: 'download_url', type: 'text', nullable: true })
  downloadUrl: string;

  @Column({ name: 'download_expires_at', type: 'timestamptz', nullable: true })
  downloadExpiresAt: Date;

  @Column({ name: 'download_count', type: 'int', default: 0 })
  downloadCount: number;

  @Column({ name: 'ip_address', type: 'inet', nullable: true })
  ipAddress: string;

  @Column({ name: 'user_agent', type: 'text', nullable: true })
  userAgent: string;

  @Index()
  @Column({ name: 'requested_at', type: 'timestamptz', default: () => 'CURRENT_TIMESTAMP' })
  requestedAt: Date;

  @Column({ name: 'completed_at', type: 'timestamptz', nullable: true })
  completedAt: Date;

  @Column({ name: 'expires_at', type: 'timestamptz', nullable: true })
  expiresAt: Date;
}
