const fs = require('fs');
const path = require('path');
const dotenv = require('dotenv');

const envPath = path.resolve(__dirname, '.env');
const envConfig = dotenv.parse(fs.readFileSync(envPath));

const newContent = `
# BILLING DATABASE (Customer Customer, Package Item)
BILLING_DB_HOST=${envConfig.DB_HOST}
BILLING_DB_USER=${envConfig.DB_USER}
BILLING_DB_PASS=${envConfig.DB_PASS}
BILLING_DB_NAME=${envConfig.DB_NAME}

# CHAT DATABASE (Logs, Queue, Contacts)
CHAT_DB_HOST=${envConfig.DB_HOST}
CHAT_DB_USER=${envConfig.DB_USER}
CHAT_DB_PASS=${envConfig.DB_PASS}
CHAT_DB_NAME=chatbot_db
`;
// Note: User previously identified 'chatbot_db' as wrong and 'billinggold' as correct.
// However, I should probably stick to what 'DB_NAME' has.
// Wait, if DB_NAME is 'billinggold', then both should be 'billinggold' initially until the user actually separates them.
// Let's use envConfig.DB_NAME for BOTH to be safe.

const safeContent = `
# --------------------------------------------------------
# SEPARATED DATABASE CONFIGURATION (Added by Auto-Migration)
# --------------------------------------------------------

# BILLING DATABASE (Customer Data)
BILLING_DB_HOST=${envConfig.DB_HOST || '127.0.0.1'}
BILLING_DB_USER=${envConfig.DB_USER || 'chatbot'}
BILLING_DB_PASS=${envConfig.DB_PASS || 'chatbot123'}
BILLING_DB_NAME=${envConfig.DB_NAME || 'billinggold'}

# CHAT DATABASE (Logs & Queue)
CHAT_DB_HOST=${envConfig.DB_HOST || '127.0.0.1'}
CHAT_DB_USER=${envConfig.DB_USER || 'chatbot'}
CHAT_DB_PASS=${envConfig.DB_PASS || 'chatbot123'}
CHAT_DB_NAME=${envConfig.DB_NAME || 'billinggold'}
`;

fs.appendFileSync(envPath, safeContent);
console.log("Updated .env successfully.");
