#!/bin/bash

# Script to fix refill-only colors that incorrectly have spool availability
# These colors should have spulna = 0 (no regular spools available)

echo "🔧 Fixing refill-only colors to have 0 spools..."

# Colors that should be refill-only
REFILL_ONLY_COLORS=(
    "Nardo Gray"
    "Blue Grey"
    "Light Gray"
    "Brown"
    "Beige"
    "Bronze"
    "Purple"
    "Cobalt Blue"
    "Turquoise"
    "Bright Green"
    "Yellow"
    "Gold"
    "Orange"
    "Maroon Red"
)

# Database connection details
DB_HOST="filamenteka.ci7fsdlbzmag.eu-central-1.rds.amazonaws.com"
DB_NAME="filamenteka"
DB_USER="filamenteka_admin"
DB_PASSWORD="onrBjiAjHKQXBAJSVWU2t2kQ7HDil9re"

echo "📊 Current status of refill-only colors:"
PGPASSWORD=$DB_PASSWORD psql -h $DB_HOST -U $DB_USER -d $DB_NAME -c "
SELECT boja, refill, spulna, kolicina 
FROM filaments 
WHERE tip = 'PLA' 
AND finish = 'Basic'
AND boja IN (
    'Nardo Gray', 'Blue Grey', 'Light Gray', 'Brown', 'Beige',
    'Bronze', 'Purple', 'Cobalt Blue', 'Turquoise', 'Bright Green',
    'Yellow', 'Gold', 'Orange', 'Maroon Red'
)
ORDER BY boja;"

echo ""
echo "🔄 Applying fix..."

# Apply the migration
PGPASSWORD=$DB_PASSWORD psql -h $DB_HOST -U $DB_USER -d $DB_NAME < database/migrations/015_fix_refill_only_colors.sql

echo ""
echo "✅ Fix applied! New status:"
PGPASSWORD=$DB_PASSWORD psql -h $DB_HOST -U $DB_USER -d $DB_NAME -c "
SELECT boja, refill, spulna, kolicina 
FROM filaments 
WHERE tip = 'PLA' 
AND finish = 'Basic'
AND boja IN (
    'Nardo Gray', 'Blue Grey', 'Light Gray', 'Brown', 'Beige',
    'Bronze', 'Purple', 'Cobalt Blue', 'Turquoise', 'Bright Green',
    'Yellow', 'Gold', 'Orange', 'Maroon Red'
)
ORDER BY boja;"

echo ""
echo "🎉 Refill-only colors have been fixed!"