import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, ManyToOne, OneToMany, JoinColumn } from 'typeorm';
import { Lesson } from '../lessons/lesson.entity';
import { ContentBlock } from '../content-blocks/content-block.entity';

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

  @Column({ type: 'bigint', name: 'lesson_id' })
  lessonId!: number;

  @ManyToOne(() => Lesson, (lesson) => lesson.activities, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'lesson_id' })
  lesson?: Lesson;

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

  @Column({ length: 255 })
  title!: string;

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

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

  /** Type-specific settings the Flutter widget consumes verbatim. */
  @Column({ type: 'json', nullable: true })
  config?: Record<string, any>;

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

  @Column({
    type: 'enum',
    enum: ['DRAFT', 'IN_REVIEW', 'PUBLISHED', 'ARCHIVED'],
    default: 'DRAFT',
  })
  status!: string;

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

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

  @OneToMany(() => ContentBlock, (block) => block.activity)
  contentBlocks?: ContentBlock[];
}
