import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { Activity } from '../activities/activity.entity';
import { DrawingTemplate } from '../drawing-templates/drawing-template.entity';

/**
 * Vector stroke data for a learner's drawing. The preview image is a
 * convenience artefact only - `strokesJson` is the source of truth so the
 * drawing stays editable (section 13 of the requirements).
 */
@Entity({ name: 'drawings' })
export class Drawing {
  @PrimaryGeneratedColumn({ type: 'bigint' })
  id!: number;

  @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: 'varchar', length: 128, nullable: true })
  anonymousSessionId?: string;

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

  @ManyToOne(() => DrawingTemplate, { onDelete: 'SET NULL', nullable: true })
  @JoinColumn({ name: 'template_id' })
  template?: DrawingTemplate;

  @Column({ type: 'int', nullable: true })
  canvasWidth?: number;

  @Column({ type: 'int', nullable: true })
  canvasHeight?: number;

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

  @Column({ type: 'json', nullable: true })
  strokesJson?: Record<string, any>;

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

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

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