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

/**
 * Raw analytics event - mirrors the collector's raw_events table.
 * Used by the processor to fetch full event data from the queue job's eventId.
 */
@Entity('raw_events')
@Index(['sessionId', 'timestamp'])
@Index(['eventType', 'timestamp'])
@Index(['processed', 'timestamp'])
export class RawEvent {
  @PrimaryGeneratedColumn('uuid')
  id!: string;

  @Column({ type: 'varchar', length: 100 })
  @Index()
  eventType!: string;

  @Column({ type: 'varchar', length: 64 })
  @Index()
  sessionId!: string;

  @Column({ type: 'varchar', length: 64, nullable: true })
  @Index()
  userId?: string | null;

  @Column({ type: 'text', nullable: true })
  pageUrl?: string | null;

  @Column({ type: 'text', nullable: true })
  referrer?: string | null;

  @Column({ type: 'varchar', length: 20, nullable: true })
  deviceType?: string | null;

  @Column({ type: 'jsonb', nullable: true })
  metadata?: Record<string, unknown> | null;

  @Column({ type: 'timestamptz' })
  @Index()
  timestamp!: Date;

  @CreateDateColumn({ type: 'timestamptz' })
  receivedAt!: Date;

  @Column({ type: 'boolean', default: false })
  @Index()
  processed!: boolean;

  @Column({ type: 'timestamptz', nullable: true })
  processedAt?: Date | null;
}
