const { Pool } = require('pg');

const connectionString = "postgresql://filamenteka_admin:onrBjiAjHKQXBAJSVWU2t2kQ7HDil9re@filamenteka.ci7fsdlbzmag.eu-central-1.rds.amazonaws.com:5432/filamenteka";

const pool = new Pool({
  connectionString,
  ssl: { rejectUnauthorized: false }
});

async function checkDatabase() {
  try {
    console.log('🔍 Checking database schema...\n');
    
    // Check columns
    const columnsResult = await pool.query(`
      SELECT column_name, data_type, is_nullable, column_default
      FROM information_schema.columns 
      WHERE table_name = 'filaments' 
      ORDER BY ordinal_position
    `);
    
    console.log('Filaments table columns:');
    console.table(columnsResult.rows);
    
    // Check if brand column exists
    const brandExists = columnsResult.rows.some(col => col.column_name === 'brand');
    console.log(`\n✅ Brand column exists: ${brandExists}`);
    
    // Get sample data
    const sampleResult = await pool.query('SELECT * FROM filaments LIMIT 1');
    console.log('\nSample filament data:');
    console.log(sampleResult.rows[0] || 'No data in table');
    
    // Test insert without brand
    console.log('\n🧪 Testing INSERT without brand field...');
    try {
      const testInsert = await pool.query(`
        INSERT INTO filaments (tip, finish, boja, boja_hex, refill, vakum, otvoreno, kolicina, cena)
        VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
        RETURNING *
      `, ['TEST_PLA', 'Basic', 'Test Color', '#FF0000', 'Ne', 'Ne', 'Ne', '1', '3999']);
      
      console.log('✅ INSERT successful! Created filament:');
      console.log(testInsert.rows[0]);
      
      // Clean up test data
      await pool.query('DELETE FROM filaments WHERE id = $1', [testInsert.rows[0].id]);
      console.log('🧹 Test data cleaned up');
      
    } catch (insertError) {
      console.log('❌ INSERT failed:', insertError.message);
      console.log('This means the database still expects the brand column!');
    }
    
  } catch (error) {
    console.error('Database check failed:', error.message);
  } finally {
    await pool.end();
  }
}

checkDatabase();