import { MigrationInterface, QueryRunner } from "typeorm";

/**
 * GPS tracking: physical trackers (gps_devices, one denormalized latest fix per
 * device for the live map) + append-only fix history (gps_positions).
 */
export class AddGpsTracking2000000000000 implements MigrationInterface {
  name = "AddGpsTracking2000000000000";

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
      CREATE TABLE IF NOT EXISTS freight.gps_devices (
        id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
        imei varchar(20) NOT NULL UNIQUE,
        name varchar,
        vehicle_id uuid REFERENCES freight.vehicles(id),
        status varchar(16) NOT NULL DEFAULT 'REGISTERED',
        last_seen_at timestamptz,
        last_lat numeric(10,6),
        last_lng numeric(10,6),
        last_speed numeric(6,2),
        last_course int,
        last_fix_at timestamptz,
        voltage_level int,
        gsm_level int,
        created_at timestamptz NOT NULL DEFAULT now(),
        updated_at timestamptz NOT NULL DEFAULT now(),
        deleted_at timestamptz
      )
    `);
    await queryRunner.query(`
      CREATE INDEX IF NOT EXISTS "IDX_GPS_DEVICES_VEHICLE"
      ON freight.gps_devices (vehicle_id)
    `);

    await queryRunner.query(`
      CREATE TABLE IF NOT EXISTS freight.gps_positions (
        id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
        device_id uuid NOT NULL,
        imei varchar(20) NOT NULL,
        vehicle_id uuid,
        lat numeric(10,6) NOT NULL,
        lng numeric(10,6) NOT NULL,
        speed numeric(6,2) NOT NULL DEFAULT 0,
        course int NOT NULL DEFAULT 0,
        satellites int NOT NULL DEFAULT 0,
        positioned boolean NOT NULL DEFAULT false,
        gps_time timestamptz NOT NULL,
        alarm int NOT NULL DEFAULT 0,
        created_at timestamptz NOT NULL DEFAULT now(),
        updated_at timestamptz NOT NULL DEFAULT now(),
        deleted_at timestamptz
      )
    `);
    await queryRunner.query(`
      CREATE INDEX IF NOT EXISTS "IDX_GPS_POSITIONS_DEVICE_TIME"
      ON freight.gps_positions (device_id, gps_time)
    `);
    await queryRunner.query(`
      CREATE INDEX IF NOT EXISTS "IDX_GPS_POSITIONS_VEHICLE_TIME"
      ON freight.gps_positions (vehicle_id, gps_time)
    `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`DROP TABLE IF EXISTS freight.gps_positions`);
    await queryRunner.query(`DROP TABLE IF EXISTS freight.gps_devices`);
  }
}
