// import 'dotenv/config';


import dotenv from 'dotenv';

// Force reload `.env` even if it was previously loaded
dotenv.config({ override: true });
import { Options } from '@mikro-orm/core';
import { PostgreSqlDriver } from '@mikro-orm/postgresql';
import { Migrator } from '@mikro-orm/migrations';

const isProduction = process.env.NODE_ENV === 'production';

const config: Options = {
    driver: PostgreSqlDriver,
    entities: [
        './dist/src/apps/_app/entities/**/*.js',
        './dist/src/apps/canvas-api/entities/**/*.js',
    ],
    entitiesTs: [
        './src/apps/_app/entities/**/*.ts',
        './src/apps/canvas-api/entities/**/*.ts',
    ],
    extensions: [Migrator],
    baseDir: process.cwd(),
    discovery: {
        warnWhenNoEntities: true,
        disableDynamicFileAccess: false,
    },
    dbName: process.env.POSTGRES_NAME,
    host: process.env.POSTGRES_HOSTNAME,
    port: Number(process.env.POSTGRES_PORT),
    // port: parseInt(process.env.POSTGRES_PORT || "5432", 10),
    user: process.env.POSTGRES_USER,
    password: process.env.POSTGRES_PASSWORD,
    debug: !isProduction,
    migrations: {
        tableName: process.env.POSTGRES_NAME,
        path: isProduction ? './dist/src/database/migrations' : './src/database/migrations',
        glob: '!(*.d).{js,ts}',
        transactional: true,
        disableForeignKeys: true,
        allOrNothing: true,
        dropTables: true,
        safe: false,
        snapshot: true,
        emit: 'ts',
    },
};

export default config;
