import * as path from 'path';
import 'dotenv/config';
import { DataSource } from 'typeorm';

// TypeORM CLI DataSource configuration
// - Used by npm scripts: migration:run / migration:revert / migration:generate
// - Keep synchronize=false, manage schema only via migrations
const AppDataSource = new DataSource({
  type: 'mysql',
  host: process.env.DB_HOST || 'localhost',
  port: Number(process.env.DB_PORT) || 3306,
  username: process.env.DB_USERNAME || 'root',
  password: process.env.DB_PASSWORD || '',
  database: process.env.DB_DATABASE || 'wwjcloud',
  synchronize: false,
  logging: false,
  // Use ts-node runtime for dev and compiled js for prod compatibility
  entities: [
    path.join(process.cwd(), 'src', '**', '*.entity.ts'),
    path.join(process.cwd(), 'dist', '**', '*.entity.js'),
  ],
  migrations: [
    path.join(process.cwd(), 'src', 'migrations', '*.{ts,js}'),
    path.join(process.cwd(), 'dist', 'migrations', '*.js'),
  ],
});

export default AppDataSource;