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

export enum ResultStatus {
  IN_PROGRESS = 'in_progress',
  COMPLETED = 'completed',
  ABANDONED = 'abandoned',
  TIMED_OUT = 'timed_out',
}

@Entity('assessment_results')
export class AssessmentResult {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  assessmentId: number;

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

  @Column()
  candidateId: number;

  @ManyToOne(() => User)
  @JoinColumn({ name: 'candidateId' })
  candidate: User;

  @Column({
    type: 'enum',
    enum: ResultStatus,
    default: ResultStatus.IN_PROGRESS,
  })
  status: ResultStatus;

  @Column({ type: 'int', default: 0 })
  totalScore: number;

  @Column({ type: 'int', default: 0 })
  maxScore: number;

  @Column({ type: 'decimal', precision: 5, scale: 2, default: 0 })
  percentage: number;

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

  @Column({ type: 'int', default: 1 })
  attemptNumber: number;

  @Column({ type: 'int', default: 0 })
  timeSpent: number; // in seconds

  @Column({ type: 'json', nullable: true })
  answers: any[]; // Array of question answers

  @Column({ type: 'json', nullable: true })
  questionResults: any[]; // Detailed results for each question

  @Column({ type: 'json', nullable: true })
  skillScores: any; // Skill-wise breakdown of scores

  @Column({ type: 'text', nullable: true })
  feedback: string; // General feedback

  @Column({ type: 'json', nullable: true })
  proctoringData: any; // Data from proctoring if enabled

  @Column({ type: 'timestamp', nullable: true })
  startedAt: Date;

  @Column({ type: 'timestamp', nullable: true })
  completedAt: Date;

  @Column({ type: 'timestamp', nullable: true })
  submittedAt: Date;

  @CreateDateColumn()
  createdAt: Date;

  @UpdateDateColumn()
  updatedAt: Date;
}
