import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { Activity } from '../activities/activity.entity';
import { Media } from '../media/media.entity';

/**
 * Annotations are stored separately from the referenced medical image so the
 * original asset is never modified (section 14 of the requirements).
 */
@Entity({ name: 'annotations' })
export class Annotation {
  @PrimaryGeneratedColumn({ type: 'bigint' })
  id!: number;

  @Column({ type: 'varchar', length: 128 })
  anonymousSessionId!: string;

  @Column({ type: 'bigint', name: 'activity_id', nullable: true })
  activityId?: number;

  @ManyToOne(() => Activity, { onDelete: 'SET NULL', nullable: true })
  @JoinColumn({ name: 'activity_id' })
  activity?: Activity;

  @Column({ type: 'bigint', name: 'media_id', nullable: true })
  mediaId?: number;

  @ManyToOne(() => Media, { onDelete: 'SET NULL', nullable: true })
  @JoinColumn({ name: 'media_id' })
  media?: Media;

  @Column({ type: 'varchar', length: 128, nullable: true })
  entityType?: string;

  @Column({ type: 'bigint', nullable: true })
  entityId?: number;

  /** Shapes: freehand, arrow, circle, rectangle, text, highlight, pin, label. */
  @Column({ type: 'json', nullable: true })
  annotationsJson?: Record<string, any>;

  @Column({ type: 'varchar', length: 1024, nullable: true })
  previewImagePath?: string;

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

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