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 }
});

const missingColors = [
  // PLA Matte - New colors with correct hex codes
  { name: "Matte Dark Chocolate", hex: "#4A3729", cena_refill: 3499, cena_spulna: 3999 },
  { name: "Matte Dark Brown", hex: "#7D6556", cena_refill: 3499, cena_spulna: 3999 },
  { name: "Matte Terracotta", hex: "#A25A37", cena_refill: 3499, cena_spulna: 3999 },
  { name: "Matte Caramel", hex: "#A4845C", cena_refill: 3499, cena_spulna: 3999 },
  { name: "Matte Dark Blue", hex: "#042F56", cena_refill: 3499, cena_spulna: 3999 },
  { name: "Matte Sky Blue", hex: "#73B2E5", cena_refill: 3499, cena_spulna: 3999 },
  { name: "Matte Ice Blue", hex: "#A3D8E1", cena_refill: 3499, cena_spulna: 3999 },
  { name: "Matte Dark Green", hex: "#68724D", cena_refill: 3499, cena_spulna: 3999 },
  { name: "Matte Grass Green", hex: "#61C680", cena_refill: 3499, cena_spulna: 3999 },
  { name: "Matte Apple Green", hex: "#C6E188", cena_refill: 3499, cena_spulna: 3999 },
  { name: "Matte Dark Red", hex: "#BB3D43", cena_refill: 3499, cena_spulna: 3999 },
  { name: "Matte Plum", hex: "#851A52", cena_refill: 3499, cena_spulna: 3999 },
  { name: "Matte Lilac Purple", hex: "#AE96D4", cena_refill: 3499, cena_spulna: 3999 },
  { name: "Matte Sakura Pink", hex: "#E8AFCF", cena_refill: 3499, cena_spulna: 3999 },
  { name: "Matte Lemon Yellow", hex: "#F7D959", cena_refill: 3499, cena_spulna: 3999 },
  { name: "Matte Bone White", hex: "#C8C5B6", cena_refill: 3499, cena_spulna: 3999 },

  // PLA Wood colors
  { name: "Ochre Yellow", hex: "#BC8B39", cena_refill: 3499, cena_spulna: 3999 },
  { name: "White Oak", hex: "#D2CCA2", cena_refill: 3499, cena_spulna: 3999 },
  { name: "Clay Brown", hex: "#8E621A", cena_refill: 3499, cena_spulna: 3999 }
];

async function addMissingColors() {
  try {
    console.log('🎨 Adding missing colors to database...\n');

    let added = 0;
    let updated = 0;
    let skipped = 0;

    for (const color of missingColors) {
      try {
        // Check if color already exists
        const existingColor = await pool.query(
          'SELECT * FROM colors WHERE name = $1',
          [color.name]
        );

        if (existingColor.rows.length > 0) {
          // Update existing color with correct hex code
          const existing = existingColor.rows[0];
          if (existing.hex !== color.hex) {
            await pool.query(
              'UPDATE colors SET hex = $1, cena_refill = $2, cena_spulna = $3 WHERE name = $4',
              [color.hex, color.cena_refill, color.cena_spulna, color.name]
            );
            console.log(`✅ Updated: ${color.name} (${existing.hex} → ${color.hex})`);
            updated++;
          } else {
            console.log(`⏭️  Skipped: ${color.name} (already exists with correct hex)`);
            skipped++;
          }
        } else {
          // Insert new color
          await pool.query(
            'INSERT INTO colors (name, hex, cena_refill, cena_spulna) VALUES ($1, $2, $3, $4)',
            [color.name, color.hex, color.cena_refill, color.cena_spulna]
          );
          console.log(`✅ Added: ${color.name} (${color.hex})`);
          added++;
        }
      } catch (error) {
        console.error(`❌ Error with ${color.name}:`, error.message);
      }
    }

    console.log(`\n📊 Summary:`);
    console.log(`   Added: ${added}`);
    console.log(`   Updated: ${updated}`);
    console.log(`   Skipped: ${skipped}`);
    console.log(`   Total: ${missingColors.length}`);

  } catch (error) {
    console.error('❌ Failed to add colors:', error.message);
  } finally {
    await pool.end();
  }
}

addMissingColors();
