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

/**
 * The canonical block types the Flutter renderer maps to widgets
 * (section 8 of the requirements). Stored as VARCHAR so administrators can
 * introduce new types without a schema change.
 */
export const CONTENT_BLOCK_TYPES = [
  'TEXT',
  'HEADING',
  'IMAGE',
  'VIDEO',
  'AUDIO',
  'INFO_BOX',
  'INSTRUCTION',
  'CLINICAL_INSIGHT',
  'QUOTE',
  'CHECKLIST',
  'DRAWING_CANVAS',
  'COLOURING_CANVAS',
  'ANNOTATION_CANVAS',
  'MEASUREMENT_TABLE',
  'FORM',
  'QUIZ',
  'MCQ',
  'DECISION_TREE',
  'CASE_SCENARIO',
  'PROCEDURE_STEP',
  'REFLECTION',
  'REFERENCE',
] as const;

export type ContentBlockType = (typeof CONTENT_BLOCK_TYPES)[number];

@Entity({ name: 'content_blocks' })
export class ContentBlock {
  @PrimaryGeneratedColumn({ type: 'bigint' })
  id!: number;

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

  @ManyToOne(() => Activity, (activity) => activity.contentBlocks, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'activity_id' })
  activity?: Activity;

  @Column({ length: 64 })
  type!: string;

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

  /** Free-form, type-specific payload rendered verbatim by the mobile client. */
  @Column({ type: 'json', nullable: true })
  payload?: Record<string, any>;

  @Column({ name: 'order_index', type: 'int', default: 0 })
  orderIndex!: number;

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

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