import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { Student } from './student.entity';
import { StudentEnrollment } from './student-enrollment.entity';
import { Exam } from './exam.entity';

@Entity('exam_scores')
export class ExamScore {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ name: 'exam_id', type: 'integer', nullable: true })
  examId: number | null;

  @ManyToOne(() => Exam, (exam) => exam.scores, { nullable: true, onDelete: 'CASCADE' })
  @JoinColumn({ name: 'exam_id' })
  exam: Exam | null;

  @Column({ name: 'student_id', type: 'integer' })
  studentId: number;

  @ManyToOne(() => Student, { eager: true })
  @JoinColumn({ name: 'student_id' })
  student: Student;

  @Column({ name: 'enrollment_id', type: 'integer', nullable: true })
  enrollmentId: number;

  @ManyToOne(() => StudentEnrollment, { nullable: true })
  @JoinColumn({ name: 'enrollment_id' })
  enrollment: StudentEnrollment;

  @Column({ name: 'exam_type', length: 50, nullable: true })
  examType: string;

  @Column({ name: 'exam_name', length: 100, nullable: true })
  examName: string;

  @Column({ length: 50, nullable: true })
  subject: string;

  @Column({ type: 'decimal', precision: 5, scale: 2, nullable: true })
  score: number | null;

  @Column({ name: 'class_avg', type: 'decimal', precision: 5, scale: 2, nullable: true })
  classAvg: number | null;

  @Column({ type: 'integer', nullable: true })
  rank: number | null;

  @Column({ name: 'exam_date', type: 'date', nullable: true })
  examDate: string;


  @Column({ type: 'varchar', length: 20, default: 'active' })
  status: 'active' | 'archived';

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

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