import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  OneToMany,
  JoinColumn,
} from 'typeorm';
import { User } from './user.entity';
import { AssessmentQuestion } from './assessment-question.entity';
import { AssessmentResult } from './assessment-result.entity';

export enum AssessmentType {
  TECHNICAL = 'technical',
  BEHAVIORAL = 'behavioral',
  COGNITIVE = 'cognitive',
  LANGUAGE = 'language',
  SOFT_SKILLS = 'soft_skills',
  DOMAIN_SPECIFIC = 'domain_specific',
}

export enum AssessmentStatus {
  DRAFT = 'draft',
  ACTIVE = 'active',
  INACTIVE = 'inactive',
  ARCHIVED = 'archived',
}

@Entity('assessments')
export class Assessment {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  title: string;

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

  @Column({
    type: 'enum',
    enum: AssessmentType,
    default: AssessmentType.TECHNICAL,
  })
  type: AssessmentType;

  @Column({
    type: 'enum',
    enum: AssessmentStatus,
    default: AssessmentStatus.DRAFT,
  })
  status: AssessmentStatus;

  @Column({ type: 'int', default: 60 })
  timeLimit: number; // in minutes

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

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

  @Column({ type: 'int', default: 50 })
  passingScore: number; // percentage

  @Column({ type: 'json', nullable: true })
  skillTags: string[]; // ['JavaScript', 'React', 'Node.js']

  @Column({ type: 'json', nullable: true })
  difficultyLevels: string[]; // ['beginner', 'intermediate', 'advanced']

  @Column({ type: 'boolean', default: true })
  allowRetake: boolean;

  @Column({ type: 'int', default: 3 })
  maxAttempts: number;

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

  @Column({ type: 'boolean', default: true })
  showResults: boolean;

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

  @Column()
  createdBy: number; // recruiter/company user ID

  @ManyToOne(() => User)
  @JoinColumn({ name: 'createdBy' })
  creator: User;

  @OneToMany(() => AssessmentQuestion, (question) => question.assessment)
  questions: AssessmentQuestion[];

  @OneToMany(() => AssessmentResult, (result) => result.assessment)
  results: AssessmentResult[];

  @CreateDateColumn()
  createdAt: Date;

  @UpdateDateColumn()
  updatedAt: Date;
}
