import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, ManyToOne, OneToMany, JoinColumn } from 'typeorm';
import { Section } from '../sections/section.entity';
import { Lesson } from '../lessons/lesson.entity';

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

  @Column({ type: 'bigint', name: 'section_id' })
  sectionId!: number;

  @ManyToOne(() => Section, (section) => section.modules, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'section_id' })
  section?: Section;

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

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

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

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

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

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

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

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

  @OneToMany(() => Lesson, (lesson) => lesson.module)
  lessons?: Lesson[];
}
