import { Entity, Column, PrimaryGeneratedColumn, CreateDateColumn, Index, Unique, OneToMany, ManyToOne, JoinColumn } from 'typeorm';
import { ApiProperty } from '@nestjs/swagger';

@Entity('emails')
@Unique('UQ_emails_email', ['email'])
export class Email {
  @ApiProperty({ description: 'Unique identifier' })
  @PrimaryGeneratedColumn()
  id: number;

  @ApiProperty({ description: 'Email address' })
  @Index('IDX_emails_email')
  @Column({ length: 255 })
  email: string;

  @ApiProperty({ description: 'Related breach records' })
  @OneToMany(() => BreachEmail, breach => breach.email)
  breaches: BreachEmail[];
}

@Entity('email_breaches')
export class BreachEmail {
  @ApiProperty({ description: 'Unique identifier' })
  @PrimaryGeneratedColumn()
  id: number;

  @ApiProperty({ description: 'Associated email' })
  @ManyToOne(() => Email, email => email.breaches)
  @JoinColumn({ name: 'email_id' })
  email: Email;

  @ApiProperty({ description: 'Source of the breach data' })
  @Column({ length: 100 })
  source: string;

  @ApiProperty({ description: 'Unique breach identifier' })
  @Column({ length: 36, name: 'breach_id' })
  breachId: string;

  @ApiProperty({ description: 'Date of the breach' })
  @Column({ type: 'timestamp', name: 'breach_date' })
  breachDate: Date;

  @ApiProperty({ description: 'Date when the record was imported' })
  @CreateDateColumn({ name: 'imported_at' })
  importedAt: Date;
}
