import mysql from 'mysql2/promise';
import dotenv from 'dotenv';

dotenv.config();

async function expandSchema() {
    const connection = await mysql.createConnection({
        host: process.env.DB_HOST,
        user: process.env.DB_USER,
        password: process.env.DB_PASS,
        database: process.env.DB_NAME,
        port: parseInt(process.env.DB_PORT || '3306')
    });

    try {
        console.log('🏗️  Expanding asset_pc table schema...');

        const columnsToAdd = [
            { name: 'location', type: 'TEXT' },
            { name: 'location_detail', type: 'TEXT' },
            { name: 'ip_address', type: 'TEXT' },
            { name: 'ip_address_2', type: 'TEXT' },
            { name: 'remote_tool', type: 'TEXT' },
            { name: 'remote_id', type: 'TEXT' },
            { name: 'remote_pw', type: 'TEXT' },
            { name: 'monitoring', type: 'TEXT' },
            { name: 'asset_purpose', type: 'TEXT' }
        ];

        for (const col of columnsToAdd) {
            try {
                await connection.query(`ALTER TABLE asset_pc ADD COLUMN \`${col.name}\` ${col.type}`);
                console.log(`✅ Added column: ${col.name}`);
            } catch (err) {
                if (err.code === 'ER_DUP_COLUMN_NAME') {
                    console.log(`ℹ️  Column ${col.name} already exists.`);
                } else {
                    throw err;
                }
            }
        }

        console.log('🎉 Schema expansion completed!');

    } catch (err) {
        console.error('❌ Schema expansion failed:', err);
    } finally {
        await connection.end();
    }
}

expandSchema();
