import {
  Column,
  Entity,
  JoinColumn,
  ManyToOne,
  PrimaryGeneratedColumn,
  Unique,
} from 'typeorm';
import { RoomInspection } from './room-inspection.entity';
import { Occupancy } from './occupancy.entity';
import { Student } from './student.entity';
import { Bed } from './bed.entity';

export type RoomInspectionStatus = 'present' | 'absent';

@Entity('room_inspection_details')
@Unique('uq_room_inspection_details_occupancy', ['inspectionId', 'occupancyId'])
export class RoomInspectionDetail {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ name: 'inspection_id' })
  inspectionId: number;

  @ManyToOne(() => RoomInspection, (inspection) => inspection.details, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'inspection_id' })
  inspection: RoomInspection;

  @Column({ name: 'occupancy_id' })
  occupancyId: number;

  @ManyToOne(() => Occupancy, { onDelete: 'RESTRICT' })
  @JoinColumn({ name: 'occupancy_id' })
  occupancy: Occupancy;

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

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

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

  @ManyToOne(() => Bed, { nullable: true, onDelete: 'SET NULL' })
  @JoinColumn({ name: 'bed_id' })
  bed: Bed | null;

  @Column({ type: 'varchar', length: 20 })
  status: RoomInspectionStatus;

  @Column({ name: 'student_name_snapshot', length: 100 })
  studentNameSnapshot: string;

  @Column({ name: 'bed_number_snapshot', type: 'varchar', length: 20, nullable: true })
  bedNumberSnapshot: string | null;
}
