import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  ManyToOne,
  JoinColumn,
  DeleteDateColumn,
  CreateDateColumn,
  UpdateDateColumn,
  OneToMany,
} from "typeorm"
import { City } from "src/modules/city/entities/city.entity"
import { State } from "src/modules/state/entities/state.entity"
import { HospitalContact } from "src/modules/hospital-contacts/entities/hospital-contact.entity"
import { TripIntermediateStop } from "src/modules/trip-intermediate-stops/entities/trip-intermediate-stop.entity"
import { Country } from "src/modules/country/entities/country.entity"
import { Customer } from "src/modules/customers/entities/customer.entity"
import { EpisodeLog } from "src/modules/customers/entities/episode_log.entity"

@Entity("hospitals")
export class Hospital {
  @PrimaryGeneratedColumn()
  id: number

  @Column({ type: "varchar" })
  name: string

  @Column({ type: "text" })
  address: string

  @Column({ type: "int" })
  city_id: number

  @Column({ type: "int" })
  state_id: number

  @Column({ type: "int", nullable: true })
  country_id: number

  @Column({ type: "varchar" })
  zip_code: string

  @Column({ type: "varchar", nullable: true })
  country_code: string

  @Column({ type: "varchar", nullable: true })
  phone_number: string

  @Column({ type: "varchar", nullable: true })
  email: string

  @Column({ type: "boolean", default: false })
  is_partner: boolean

  @Column({ type: "varchar", nullable: true })
  latitude: string

  @Column({ type: "varchar", nullable: true })
  longitude: string

  @Column({ type: "varchar", nullable: true })
  place_id: string // google location place id for the hospital

  @Column({ type: "varchar", default: "active" })
  status: string

  @CreateDateColumn({ type: "timestamp", default: () => "NOW()" })
  created_at: Date

  @UpdateDateColumn({ type: "timestamp", default: () => "NOW()" })
  updated_at: Date

  @DeleteDateColumn()
  deleted_at: Date

  @ManyToOne(() => City, { onDelete: "RESTRICT" })
  @JoinColumn({ name: "city_id" })
  city: City

  @ManyToOne(() => State, { onDelete: "RESTRICT" })
  @JoinColumn({ name: "state_id" })
  state: State

  @ManyToOne(() => Country, { onDelete: "RESTRICT" })
  @JoinColumn({ name: "country_id" })
  country: Country

  @OneToMany(() => HospitalContact, (contact) => contact.hospital)
  contacts: HospitalContact[]

  @OneToMany(() => Customer, (customer) => customer.hospital)
  customer: Customer[]

  @OneToMany(() => EpisodeLog, (log) => log.hospital)
  logs: EpisodeLog[]

  @OneToMany(
    () => TripIntermediateStop,
    (trip_intermediate_stop) => trip_intermediate_stop.hospital,
  )
  trip_intermediate_stop: TripIntermediateStop[]
}
