import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { Assessment } from './assessment.entity';

export enum QuestionType {
  MULTIPLE_CHOICE = 'multiple_choice',
  SINGLE_CHOICE = 'single_choice',
  TRUE_FALSE = 'true_false',
  FILL_IN_BLANK = 'fill_in_blank',
  CODING = 'coding',
  ESSAY = 'essay',
  FILE_UPLOAD = 'file_upload',
  DRAG_DROP = 'drag_drop',
  MATCHING = 'matching',
}

export enum DifficultyLevel {
  BEGINNER = 'beginner',
  INTERMEDIATE = 'intermediate',
  ADVANCED = 'advanced',
  EXPERT = 'expert',
}

@Entity('assessment_questions')
export class AssessmentQuestion {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  assessmentId: number;

  @ManyToOne(() => Assessment, (assessment) => assessment.questions, {
    onDelete: 'CASCADE',
  })
  @JoinColumn({ name: 'assessmentId' })
  assessment: Assessment;

  @Column({ type: 'text' })
  question: string;

  @Column({
    type: 'enum',
    enum: QuestionType,
    default: QuestionType.MULTIPLE_CHOICE,
  })
  type: QuestionType;

  @Column({
    type: 'enum',
    enum: DifficultyLevel,
    default: DifficultyLevel.INTERMEDIATE,
  })
  difficulty: DifficultyLevel;

  @Column({ type: 'json', nullable: true })
  options: string[]; // For multiple choice, single choice questions

  @Column({ type: 'json', nullable: true })
  correctAnswers: any[]; // Can be string, number, or array for multiple correct answers

  @Column({ type: 'text', nullable: true })
  explanation: string; // Explanation for the correct answer

  @Column({ type: 'int', default: 1 })
  points: number; // Points for this question

  @Column({ type: 'int', default: 0 })
  timeLimit: number; // Time limit in seconds for this specific question

  @Column({ type: 'json', nullable: true })
  codeTemplate: any; // For coding questions - language, starter code, etc.

  @Column({ type: 'json', nullable: true })
  testCases: any[]; // For coding questions - input/output test cases

  @Column({ type: 'json', nullable: true })
  mediaFiles: string[]; // URLs to images, videos, or other media

  @Column({ type: 'text', nullable: true })
  hint: string; // Optional hint for the question

  @Column({ type: 'boolean', default: false })
  isRequired: boolean;

  @Column({ type: 'int', default: 0 })
  order: number; // Order of question in assessment

  @Column({ type: 'json', nullable: true })
  tags: string[]; // Skill tags for this question

  @CreateDateColumn()
  createdAt: Date;

  @UpdateDateColumn()
  updatedAt: Date;
}
