import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { User } from './user.entity';

export enum DeletionRequestType {
  FULL_DELETION = 'full_deletion',
  PARTIAL_DELETION = 'partial_deletion',
  ANONYMIZATION = 'anonymization',
}

export enum DeletionRequestStatus {
  PENDING = 'pending',
  IN_PROGRESS = 'in_progress',
  COMPLETED = 'completed',
  FAILED = 'failed',
  CANCELLED = 'cancelled',
}

export type DataType =
  | 'activities'
  | 'photos'
  | 'ai_conversations'
  | 'children'
  | 'family'
  | 'audit_logs'
  | 'profile';

@Entity('data_deletion_requests')
export class DataDeletionRequest {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ name: 'user_id' })
  userId: string;

  @ManyToOne(() => User, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'user_id' })
  user: User;

  @Column({
    type: 'varchar',
    length: 50,
    name: 'request_type',
    default: DeletionRequestType.FULL_DELETION,
  })
  requestType: DeletionRequestType;

  @Column({ type: 'text', nullable: true })
  reason: string;

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

  @Column({
    type: 'varchar',
    length: 50,
    default: DeletionRequestStatus.PENDING,
  })
  status: DeletionRequestStatus;

  @Column({ type: 'timestamp', name: 'scheduled_deletion_date', nullable: true })
  scheduledDeletionDate: Date;

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

  @Column({ type: 'jsonb', name: 'data_types', nullable: true })
  dataTypes: DataType[];

  @Column({ name: 'processor_id', nullable: true })
  processorId: string;

  @ManyToOne(() => User, { nullable: true })
  @JoinColumn({ name: 'processor_id' })
  processor: User;

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

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

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

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

  @Column({
    type: 'varchar',
    length: 255,
    name: 'deletion_confirmation_token',
    unique: true,
    nullable: true,
  })
  deletionConfirmationToken: string;

  @Column({ type: 'timestamp', name: 'confirmed_at', nullable: true })
  confirmedAt: Date;

  @Column({ type: 'jsonb', nullable: true })
  metadata: Record<string, any>;

  @CreateDateColumn({ name: 'created_at' })
  createdAt: Date;

  @UpdateDateColumn({ name: 'updated_at' })
  updatedAt: Date;

  // Helper methods
  isPending(): boolean {
    return this.status === DeletionRequestStatus.PENDING;
  }

  isCompleted(): boolean {
    return this.status === DeletionRequestStatus.COMPLETED;
  }

  isConfirmed(): boolean {
    return this.confirmedAt !== null;
  }

  canBeProcessed(): boolean {
    return this.isConfirmed() && this.status === DeletionRequestStatus.PENDING;
  }
}
