import { BaseEntity } from "@edr/api-common";
import { Column, Entity, Index } from "typeorm";

/**
 * A physical GPS tracker (GT06), keyed by IMEI. Same table as
 * @edr/freight-api's GpsDevice; the Vehicle relation is intentionally dropped
 * here — the ingester only needs the `vehicleId` column to stamp positions, not
 * the Vehicle entity graph.
 */
@Entity({ name: "gps_devices", schema: "freight" })
@Index(["vehicleId"])
export class GpsDevice extends BaseEntity {
  @Column({ name: "imei", type: "varchar", length: 20, unique: true })
  imei!: string;

  @Column({ name: "name", type: "varchar", nullable: true })
  name?: string | null;

  @Column({ name: "vehicle_id", type: "uuid", nullable: true })
  vehicleId?: string | null;

  @Column({ name: "status", type: "varchar", length: 16, default: "REGISTERED" })
  status!: string;

  @Column({ name: "last_seen_at", type: "timestamptz", nullable: true })
  lastSeenAt?: Date | null;

  @Column({ name: "last_lat", type: "numeric", precision: 10, scale: 6, nullable: true })
  lastLat?: number | null;

  @Column({ name: "last_lng", type: "numeric", precision: 10, scale: 6, nullable: true })
  lastLng?: number | null;

  @Column({ name: "last_speed", type: "numeric", precision: 6, scale: 2, nullable: true })
  lastSpeed?: number | null;

  @Column({ name: "last_course", type: "int", nullable: true })
  lastCourse?: number | null;

  @Column({ name: "last_fix_at", type: "timestamptz", nullable: true })
  lastFixAt?: Date | null;

  @Column({ name: "voltage_level", type: "int", nullable: true })
  voltageLevel?: number | null;

  @Column({ name: "gsm_level", type: "int", nullable: true })
  gsmLevel?: number | null;
}
