import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
  Check,
} from 'typeorm';

export enum ScheduleType {
  INTERNAL = 'INTERNAL',
  RENTAL = 'RENTAL',
}

@Entity('class_schedule')
@Check('week_day BETWEEN 1 AND 7')
export class ClassSchedule {
  @PrimaryGeneratedColumn()
  id: number;

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

  // Forward reference — Class entity
  @ManyToOne('Class', { nullable: true })
  @JoinColumn({ name: 'class_id' })
  class: unknown;

  @Column({ name: 'classroom_id', type: 'integer' })
  classroomId: number;

  // Forward reference — Classroom entity
  @ManyToOne('Classroom')
  @JoinColumn({ name: 'classroom_id' })
  classroom: unknown;

  @Column({ name: 'week_day', type: 'integer' })
  weekDay: number;

  @Column({ name: 'start_time', length: 5 })
  startTime: string;

  @Column({ name: 'end_time', length: 5 })
  endTime: string;

  /** 课程开始前允许计入签到的分钟数。 */
  @Column({ name: 'attendance_advance_minutes', type: 'integer', default: 30 })
  attendanceAdvanceMinutes: number;

  @Column({ name: 'start_date', type: 'date' })
  startDate: string;

  @Column({ name: 'end_date', type: 'date' })
  endDate: string;

  @Column({ name: 'subject', length: 50 })
  subject: string;

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

  // Forward reference — User entity
  @ManyToOne('User', { nullable: true })
  @JoinColumn({ name: 'teacher_id' })
  teacher: unknown;

  @Column({ name: 'schedule_type', length: 20, default: 'INTERNAL' })
  scheduleType: string;

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

  @Column({ name: 'status', length: 20, default: 'active' })
  status: string;

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

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

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

}
