import {
  Column,
  CreateDateColumn,
  Entity,
  JoinColumn,
  ManyToOne,
  OneToMany,
  PrimaryGeneratedColumn,
  Unique,
  UpdateDateColumn,
} from 'typeorm';
import { Room } from './room.entity';
import { User } from './user.entity';
import { RoomInspectionDetail } from './room-inspection-detail.entity';

export type RoomInspectionSource = 'manual' | 'automatic';

@Entity('room_inspections')
@Unique('uq_room_inspections_date_room', ['inspectionDate', 'roomId'])
export class RoomInspection {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ name: 'inspection_date', type: 'date' })
  inspectionDate: string;

  @Column({ name: 'room_id' })
  roomId: number;

  @ManyToOne(() => Room, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'room_id' })
  room: Room;

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

  @ManyToOne(() => User, { nullable: true, onDelete: 'SET NULL' })
  @JoinColumn({ name: 'inspector_id' })
  inspector: User | null;

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

  @Column({ type: 'varchar', length: 20, default: 'manual' })
  source: RoomInspectionSource;

  @Column({ name: 'submitted_at', type: 'datetime' })
  submittedAt: Date;

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

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

  @OneToMany(() => RoomInspectionDetail, (detail) => detail.inspection)
  details: RoomInspectionDetail[];
}
