import { registerAs } from "@nestjs/config";
import { TypeOrmModuleOptions } from "@nestjs/typeorm";

import { GpsDevice } from "../gps/entities/gps-device.entity";
import { GpsPosition } from "../gps/entities/gps-position.entity";

/**
 * DB config for the GPS ingester. Points at the SAME database as
 * @edr/freight-api and touches only the two GPS tables, which are explicitly
 * schema-qualified to `freight` on the entities — so no search_path handler is
 * needed here. This app NEVER runs migrations or synchronize: @edr/freight-api
 * owns the schema (the AddGpsTracking migration creates these tables).
 */
export default registerAs("database", (): TypeOrmModuleOptions => {
  return {
    type: "postgres",
    host: process.env.DB_HOST ?? "localhost",
    port: parseInt(process.env.DB_PORT ?? "5433", 10),
    username: process.env.DB_USER ?? "postgres",
    password: process.env.DB_PASSWORD ?? "",
    database: process.env.DB_NAME ?? "edr_freight",
    entities: [GpsDevice, GpsPosition],
    migrations: [],
    migrationsRun: false,
    synchronize: false,
    logging: process.env.TYPEORM_LOGGING === "true" ? true : ["error", "warn"],
  };
});
