import { registerAs } from "@nestjs/config";
import { TypeOrmModuleOptions } from "@nestjs/typeorm";
import { join, dirname } from "path";
import {
  DefaultPosition,
  DefaultUnit,
  EmployeePosition,
  Employee,
  OrganizationConfiguration,
  GlobalOrganizationConfiguration,
  OrganizationType,
  Organization,
  PositionConfiguration,
  PositionPermission,
  PositionTypeConfiguration,
  PositionTypePermission,
  PositionType,
  Position,
  Project,
  GlobalUnitConfiguration,
  Unit,
  EmployeeSignature,
  EmployeeStamp,
  RecordFooter,
  RecordHeader,
  Seal,
  AccountConfiguration,
  Application,
  DocumentaryRequirement,
  Permission,
  RolePermission,
  Role,
  Session,
  UserCredential,
  UserDocument,
  UserRole,
  UserVerification,
  User,
  Notification,
  NotificationEvent,
  NotificationPlaceholder,
  NotificationReceiverField,
  NotificationReceiver,
  NotificationTemplate,
} from "@tria-plc/iamapi-common";
import { OrganizationSetting } from "@tria-plc/iamapi-common/entities/iam/organization-structure/organization-setting.entity";
import { APPLICATION_SEARCH_PATH } from "./ensure-postgres-schemas";

const iamEntities = [
  DefaultPosition,
  DefaultUnit,
  EmployeePosition,
  Employee,
  OrganizationConfiguration,
  GlobalOrganizationConfiguration,
  OrganizationSetting,
  OrganizationType,
  Organization,
  PositionConfiguration,
  PositionPermission,
  PositionTypeConfiguration,
  PositionTypePermission,
  PositionType,
  Position,
  Project,
  GlobalUnitConfiguration,
  Unit,
  EmployeeSignature,
  EmployeeStamp,
  RecordFooter,
  RecordHeader,
  Seal,
  AccountConfiguration,
  Application,
  DocumentaryRequirement,
  Permission,
  RolePermission,
  Role,
  Session,
  UserCredential,
  UserDocument,
  UserRole,
  UserVerification,
  User,
  Notification,
  NotificationEvent,
  NotificationPlaceholder,
  NotificationReceiverField,
  NotificationReceiver,
  NotificationTemplate,
];

const iamMigrationsGlob = join(
  dirname(require.resolve("@tria-plc/iamapi-common/package.json")),
  "dist/db/migrations/*.js",
);
const freightMigrationsGlob = join(__dirname, "../migrations/*.js");

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",
    schema: "public",
    extra: {
      options: `-c search_path=${APPLICATION_SEARCH_PATH}`,
    },
    entities: [__dirname + "/../**/*.entity.{ts,js}", ...iamEntities],
    autoLoadEntities: true,
    migrations: [
      // IAM schema + tables must be created before freight migrations
      iamMigrationsGlob,
      freightMigrationsGlob,
    ],
    migrationsRun: true,
    // Schema changes via migrations only (synchronize breaks ITMLS backfill on existing rows).
    synchronize: false,
    logging: process.env.NODE_ENV === "development",
  };
});
