import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  ManyToOne,
  JoinColumn,
  CreateDateColumn,
} from 'typeorm';
import { MockTestAttempts } from '../../mock_test_attempts/entities/mock_test_attempts.entity';
import { QuestionBank } from '../../question_bank/entities/question_bank.entity';
import { McqOptions } from '../../mcq_options/entities/mcq_options.entity';

@Entity('candidate_answers')
export class CandidateAnswer {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  attempt_id: number;

  @Column()
  candidate_id: number;

  @Column()
  question_id: number;

  @Column({ nullable: true })
  selected_option_id: number;

  @Column({ default: false })
  is_correct: boolean;

  @CreateDateColumn()
  answered_at: Date;

  @ManyToOne(() => MockTestAttempts, (attempt) => attempt.answers, {
    onDelete: 'CASCADE',
  })
  @JoinColumn({ name: 'attempt_id' })
  attempt: MockTestAttempts;

  @ManyToOne(() => QuestionBank, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'question_id' })
  question: QuestionBank;

  @ManyToOne(() => McqOptions, { onDelete: 'SET NULL', nullable: true })
  @JoinColumn({ name: 'selected_option_id' })
  selected_option: McqOptions;
}
