/**
 * TypeORM Data Source Configuration
 *
 * Used for migrations and CLI commands
 */

import { DataSource } from 'typeorm';
import { config } from 'dotenv';
import { join } from 'path';

// Load environment variables
config();

export const AppDataSource = new DataSource({
  type: 'postgres',
  host: process.env.DATABASE_HOST || 'localhost',
  port: parseInt(process.env.DATABASE_PORT || '5432', 10),
  username: process.env.DATABASE_USER || 'xpeditis',
  password: process.env.DATABASE_PASSWORD || 'xpeditis_dev_password',
  database: process.env.DATABASE_NAME || 'xpeditis_dev',
  entities: [join(__dirname, 'entities', '*.orm-entity.{ts,js}')],
  migrations: [join(__dirname, 'migrations', '*.{ts,js}')],
  subscribers: [],
  synchronize: false, // Never use in production
  logging: process.env.NODE_ENV === 'development',
  ssl: process.env.DATABASE_SSL === 'true' ? { rejectUnauthorized: false } : false,
});
