/**
 * Enlarges users.present_lat_lng / permanent_lat_lng / office_lat_lng to at least VARCHAR(500).
 * If a column does not exist, it will be created as VARCHAR(500) NULL.
 * If a column exists with smaller length, it will be altered to VARCHAR(500).
 * If the column is TEXT/LONGTEXT already, it will be left as-is (already >= 500).
 *
 * Run with:
 *   node src/scripts/alter_latlng_columns.js
 */
const { pool } = require('../config/database');
const COLUMNS = [
    'present_lat_lng',
    'permanent_lat_lng',
    'office_lat_lng'
];
async function ensureColumnAtLeastVarchar500(column) {
    const checkSql = `
    SELECT DATA_TYPE, CHARACTER_MAXIMUM_LENGTH
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE()
      AND TABLE_NAME = 'users'
      AND COLUMN_NAME = ?
  `;
    const [rows] = await pool.query(checkSql, [column]);
    if (rows.length === 0) {
        const addSql = `ALTER TABLE users ADD COLUMN \`${column}\` VARCHAR(500) NULL`;
        await pool.query(addSql);
        console.log(`✅ Added column ${column} as VARCHAR(500) NULL`);
        return;
    }
    const info = rows[0];
    const dataType = String(info.DATA_TYPE || '').toLowerCase();
    const maxLen = info.CHARACTER_MAXIMUM_LENGTH;
    // If it's text/longtext/mediumtext, it's already large enough; skip
    if (dataType.includes('text')) {
        console.log(`ℹ️  ${column} is ${dataType}, no change needed.`);
        return;
    }
    if (dataType === 'varchar') {
        const len = Number(maxLen || 0);
        if (!Number.isNaN(len) && len >= 500) {
            console.log(`ℹ️  ${column} is already VARCHAR(${len}), no change needed.`);
            return;
        }
    }
    const alterSql = `ALTER TABLE users MODIFY COLUMN \`${column}\` VARCHAR(500) NULL`;
    await pool.query(alterSql);
    console.log(`✅ Altered ${column} to VARCHAR(500) NULL (was ${dataType}${maxLen ? '(' + maxLen + ')' : ''})`);
}
async function run() {
    try {
        for (const col of COLUMNS) {
            await ensureColumnAtLeastVarchar500(col);
        }
        console.log('🎉 Lat/Lng columns ensured to be at least VARCHAR(500).');
        process.exit(0);
    }
    catch (e) {
        console.error('❌ Error altering columns:', e);
        process.exit(1);
    }
}
run();
//# sourceMappingURL=alter_latlng_columns.js.map