import { DataSource, DataSourceOptions } from 'typeorm';
import * as dotenv from 'dotenv';
import * as path from 'path';

// Load .env from project root
dotenv.config({ path: path.resolve(__dirname, '../../../../.env') });

const options: DataSourceOptions = {
  type: 'postgres',
  host: process.env.DB_HOST || 'localhost',
  port: parseInt(process.env.DB_PORT || '5432', 10),
  username: process.env.DB_USERNAME || 'it0',
  password: process.env.DB_PASSWORD || 'it0_dev',
  database: process.env.DB_DATABASE || 'it0',
  logging: process.env.DB_LOGGING === 'true',
  // Entities are loaded per-service; this data source is for migrations only
  entities: [],
  migrations: [],
};

export const AppDataSource = new DataSource(options);
