import { City } from "src/modules/city/entities/city.entity"
import { ClientCompanyContact } from "src/modules/client-company-contacts/entities/client-company-contact.entity"
import { ClientsCompany } from "src/modules/clients-companies/entities/clients-company.entity"
import { Country } from "src/modules/country/entities/country.entity"
import { Hospital } from "src/modules/hospitals/entities/hospital.entity"
import { State } from "src/modules/state/entities/state.entity"
import { VehicleType } from "src/modules/vehicle-type/entities/vehicle-type.entity"
import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  DeleteDateColumn,
  ManyToOne,
  JoinColumn,
  ManyToMany,
  JoinTable,
  OneToMany,
  AfterLoad,
} from "typeorm"
import { Escort } from "src/modules/escorts/entities/escort.entity"
import { TeamMember } from "src/modules/team-member/entities/team_member.entity"
import { CustomerPhoneNumbers } from "./customer_phone_number.entity"
import { TripCancellation } from "src/modules/trips/entities/trip-cancellations.entity"
import { Rating } from "src/modules/rating/entities/rating.entity"
import { CustomerTag } from "src/modules/customers-tags/entities/customer_tags.entity"
import { CustomerLogin } from "./customer_login.entity"
import { CustomerTreatmentPlan } from "src/modules/customer-treatment-plan/entities/customer_treatment_plan.entity"
import { Invoice } from "src/modules/invoices/entities/invoice.entity"
import { CustomerEpisode } from "./customer_episode.entity"
import { CustomerNotification } from "src/modules/notification/entities/customer-notification.entity"
import { TripTimelineHistory } from "src/modules/trips/entities/trip-timeline.entity"

@Entity("customers")
export class Customer {
  @PrimaryGeneratedColumn()
  id: number

  @Column({ type: "int", nullable: true })
  client_company_id: number | null

  @Column({ type: "int", nullable: true })
  client_contact_id: number | null

  @Column({ type: "int", nullable: true })
  city_id: number | null

  @Column({ type: "int", nullable: true })
  pricing_city_id: number | null

  @Column({ type: "int", nullable: true })
  state_id: number | null

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

  @Column({ type: "int", nullable: true })
  hospital_id: number | null

  @Column({ type: "int", nullable: true })
  vehicle_type_id: number | null

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

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

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

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

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

  @Column({ type: "varchar", nullable: true })
  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: "varchar", nullable: true })
  visa_start_date: string

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

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

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

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

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

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

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

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

  @Column({ type: "int", default: 1 })
  current_step: number

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

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

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

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

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

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

  @Column({ type: "timestamp", nullable: true })
  customer_otp_expires_at: Date

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

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

  @DeleteDateColumn({ type: "timestamp", nullable: true })
  deleted_at: Date

  @AfterLoad()
  async setProfilePhotoPrefix() {
    if (
      this.profile_photo &&
      this.profile_photo !== "null" &&
      this.profile_photo !== "undefined"
    ) {
      this.profile_photo = `${process.env.BACK_URL}/${this.profile_photo}`
    }
  }

  @ManyToOne(() => ClientsCompany, (client) => client.customer)
  @JoinColumn({ name: "client_company_id" })
  client_company: ClientsCompany

  @ManyToOne(
    () => ClientCompanyContact,
    (client_contact) => client_contact.customer,
  )
  @JoinColumn({ name: "client_contact_id" })
  client_contact: ClientCompanyContact

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

  @ManyToOne(() => City, (city) => city.pricing_customers)
  @JoinColumn({ name: "pricing_city_id" })
  pricing_city: City

  @OneToMany(() => CustomerEpisode, (episode) => episode.customer)
  episodes: CustomerEpisode[]

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

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

  @ManyToOne(() => Hospital, (hospital) => hospital.customer)
  @JoinColumn({ name: "hospital_id" })
  hospital: Hospital

  @ManyToOne(() => VehicleType, (vehicle_type) => vehicle_type.customer)
  @JoinColumn({ name: "vehicle_type_id" })
  vehicle_type: VehicleType

  @ManyToMany(() => CustomerTag, (tag) => tag.customers, { cascade: true })
  @JoinTable({
    name: "customer_tags",
    joinColumn: { name: "customer_id", referencedColumnName: "id" },
    inverseJoinColumn: { name: "tag_id", referencedColumnName: "id" },
  })
  tags: CustomerTag[]

  @OneToMany(() => Escort, (escort) => escort.customer)
  escort: Escort[]

  @OneToMany(() => CustomerPhoneNumbers, (phone) => phone.customer, {
    cascade: true,
  })
  phone_numbers: CustomerPhoneNumbers[]

  @OneToMany(
    () => TripCancellation,
    (cancellation) => cancellation.cancel_customer,
  )
  cancellations: TripCancellation[]

  @ManyToOne(() => TeamMember, (dispatcher) => dispatcher.customer)
  @JoinColumn({ name: "dispatcher_id" })
  dispatcher: TeamMember

  @OneToMany(() => Rating, (rating) => rating.rater_customer)
  givenRatings: Rating[]

  @OneToMany(() => Rating, (rating) => rating.rated_customer)
  receivedRatings: Rating[]

  @OneToMany(() => CustomerLogin, (login) => login.customer)
  customer_logins: CustomerLogin[]

  @OneToMany(() => CustomerTreatmentPlan, (plan) => plan.customer)
  treatment_plans: CustomerTreatmentPlan[]

  @OneToMany(() => Invoice, (invoices) => invoices.customer)
  invoices: Invoice[]

  @OneToMany(
    () => CustomerNotification,
    (notification) => notification.customer,
  )
  customerNotifications: CustomerNotification[]

  @OneToMany(() => TripTimelineHistory, (timeline) => timeline.customer)
  timeline: TripTimelineHistory[]
}
