import {
  Column,
  CreateDateColumn,
  Entity,
  Index,
  JoinColumn,
  ManyToOne,
  OneToMany,
  PrimaryGeneratedColumn,
  UpdateDateColumn,
} from 'typeorm';
import { ClassSchedule } from './class-schedule.entity';
import { Class } from './class.entity';
import { AttendanceRecord } from './attendance-record.entity';

@Entity('attendance_sessions')
@Index(['scheduleId', 'lessonDate'], { unique: true })
export class AttendanceSession {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ name: 'schedule_id', type: 'integer' })
  scheduleId: number;

  @ManyToOne(() => ClassSchedule, { onDelete: 'RESTRICT' })
  @JoinColumn({ name: 'schedule_id' })
  schedule: ClassSchedule;

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

  @ManyToOne(() => Class, { onDelete: 'RESTRICT' })
  @JoinColumn({ name: 'class_id' })
  class: Class;

  @Column({ name: 'lesson_date', type: 'date' })
  lessonDate: string;

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

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

  @Column({ name: 'started_at', type: 'datetime', nullable: true })
  startedAt: Date | null;

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

  @Column({ name: 'completed_at', type: 'datetime', nullable: true })
  completedAt: Date | null;

  @OneToMany(() => AttendanceRecord, (record) => record.attendanceSession)
  records: AttendanceRecord[];

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

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