import { IncidentReporting } from "src/modules/incident-reporting/entities/incident-reporting.entity"
import { InspectionFleetReport } from "src/modules/inspection-questions/entities/inspection-fleet-report.entity"
import { State } from "src/modules/state/entities/state.entity"
import { TeamMember } from "src/modules/team-member/entities/team_member.entity"
import { TripFleetAssignment } from "src/modules/trips/entities/fleet-trip-management.entity"
import { VehicleInsurance } from "src/modules/vehicle-insurance/entities/vehicle-insurance.entity"
import { VehicleMaintenance } from "src/modules/vehicle-maintenance/entities/vehicle-maintenance.entity"
import { VehicleManufacturer } from "src/modules/vehicle-manufacturer/entities/vehicle-manufacturer.entity"
import { VehicleModel } from "src/modules/vehicle-model/entities/vehicle-model.entity"
import { VehicleStatus } from "src/modules/vehicle-status/entities/vehicle-status.entity"
import { VehicleType } from "src/modules/vehicle-type/entities/vehicle-type.entity"
import {
  AfterLoad,
  Column,
  CreateDateColumn,
  DeleteDateColumn,
  Entity,
  JoinColumn,
  ManyToOne,
  OneToMany,
  PrimaryGeneratedColumn,
  UpdateDateColumn,
} from "typeorm"
import { DriverFleetHistory } from "./driver-fleet-history.entity"
import { City } from "src/modules/city/entities/city.entity"

@Entity("fleet_operations")
export class FleetManagement {
  @PrimaryGeneratedColumn()
  id: number

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  @ManyToOne(
    () => VehicleManufacturer,
    (vehicleManufacturer) => vehicleManufacturer.fleetManagements,
  )
  @JoinColumn({ name: "vehicle_manufacture_id" })
  vehicle_manufacture: VehicleManufacturer

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

  //have to change this relation naming convention
  @ManyToOne(
    () => VehicleModel,
    (vehicleModel) => vehicleModel.fleetManagements,
  )
  @JoinColumn({ name: "vehicle_model_id" })
  vehicleModel: VehicleModel

  @ManyToOne(() => TeamMember)
  @JoinColumn({ name: "assigned_driver" })
  assignedDriver: TeamMember

  @ManyToOne(
    () => TeamMember,
    (assigned_dispatcher) => assigned_dispatcher.fleetManagement,
  )
  @JoinColumn({ name: "assigned_dispatcher_id" })
  assigned_dispatcher: TeamMember

  @ManyToOne(() => VehicleStatus, (status) => status.fleetManagements)
  @JoinColumn({ name: "vehicle_status_id" })
  vehicle_status: VehicleStatus

  @ManyToOne(() => State, (state) => state.fleet)
  @JoinColumn({ name: "state_id" })
  state_details: State

  @ManyToOne(() => State, (location) => location.fleet_location)
  @JoinColumn({ name: "vehicle_location_id" })
  vehicle_location: State

  @ManyToOne(() => City, (location) => location.fleet_location)
  @JoinColumn({ name: "vehicle_city_location_id" })
  vehicle_city_location: City

  @OneToMany(
    () => VehicleInsurance,
    (VehicleInsurance) => VehicleInsurance.fleet,
  )
  vehicleInsurances: VehicleInsurance[]

  @OneToMany(
    () => VehicleMaintenance,
    (VehicleMaintenance) => VehicleMaintenance.fleet,
  )
  vehicleMaintenances: VehicleMaintenance[]

  @OneToMany(() => TripFleetAssignment, (assignment) => assignment.fleet)
  assignments: TripFleetAssignment[]

  @OneToMany(() => DriverFleetHistory, (history) => history.fleet)
  fleet_history: DriverFleetHistory[]

  @OneToMany(() => InspectionFleetReport, (report) => report.fleet)
  inspection_reports: InspectionFleetReport[]

  @OneToMany(
    () => IncidentReporting,
    (incident_reporting) => incident_reporting.fleet_management,
  )
  incident_reporting: IncidentReporting[]

  @AfterLoad()
  async registrationDocument() {
    if (this.registration_document) {
      this.registration_document =
        process.env.FILE_UPLOAD === "Local"
          ? `${process.env.BACK_URL}/${this.registration_document}`
          : `${process.env.CLOUDFLARE_R2_PUBLIC_URL}/${this.registration_document}`
    }
  }
}
