import { Options } from '@mikro-orm/core';
import { PostgreSqlDriver } from '@mikro-orm/postgresql';
import { Migrator } from '@mikro-orm/migrations';
import dotenv from 'dotenv';

if (process.env.KUBERNETES_SERVICE_HOST === undefined) {
    dotenv.config({ override: true });
}

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 || 'fusero-boilerplate-db',
    host: process.env.POSTGRES_HOSTNAME || 'localhost',
    port: Number(process.env.POSTGRES_PORT) || 5432,
    user: process.env.POSTGRES_USER || 'root',
    password: process.env.POSTGRES_PASSWORD || 'root123',
    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;
