import dotenv from 'dotenv';
import path from 'path';
import { fileURLToPath } from 'url';

// Ensure .env is loaded from the backend folder (not dependent on process.cwd())
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
dotenv.config({ path: path.resolve(__dirname, '.env') });

const isTestEnv = process.env.NODE_ENV === 'test';

const baseConfig = {
  username: process.env.DB_USER || 'root',
  password: process.env.DB_PASSWORD || 'hitomisan',
  database: process.env.DB_NAME || 'trainingdiary',
  host: process.env.DB_HOST || 'localhost',
  dialect: process.env.DB_DIALECT || 'mysql',
  define: {
    freezeTableName: true,
    underscored: true,
    underscoredAll: true,
  },
  logging: false,
  storage: process.env.DB_STORAGE,
};

if (isTestEnv) {
  baseConfig.username = 'sqlite';
  baseConfig.password = '';
  baseConfig.database = 'sqlite';
  baseConfig.host = 'localhost';
  baseConfig.dialect = 'sqlite';
  baseConfig.storage = process.env.DB_STORAGE || ':memory:';
}

if (baseConfig.dialect === 'sqlite' && !baseConfig.storage) {
  baseConfig.storage = ':memory:';
}

export const development = baseConfig;
 