#!/bin/bash
# Database Schema Fix Script
# Run with proper PostgreSQL credentials

export PGPASSWORD='MySecurePass123'
PSQL="psql -h 192.168.10.130 -U songlyric_user -d church_songlyric"

echo "============================================================"
echo "DATABASE SCHEMA FIX SCRIPT"
echo "============================================================"

# Add missing indexes on songs table
echo ""
echo "📊 Adding indexes on songs table..."
$PSQL -c "CREATE INDEX IF NOT EXISTS idx_song_title ON songs(title);" 2>&1 | grep -v "already exists" || echo "  ✅ idx_song_title created"
$PSQL -c "CREATE INDEX IF NOT EXISTS idx_song_artist ON songs(artist);" 2>&1 | grep -v "already exists" || echo "  ✅ idx_song_artist created"
$PSQL -c "CREATE INDEX IF NOT EXISTS idx_song_band ON songs(band);" 2>&1 | grep -v "already exists" || echo "  ✅ idx_song_band created"

# Add missing indexes on plans table
echo ""
echo "📊 Adding indexes on plans table..."
$PSQL -c "CREATE INDEX IF NOT EXISTS idx_plan_date ON plans(date);" 2>&1 | grep -v "already exists" || echo "  ✅ idx_plan_date created"
$PSQL -c "CREATE INDEX IF NOT EXISTS idx_plan_profile ON plans(profile_id);" 2>&1 | grep -v "already exists" || echo "  ✅ idx_plan_profile created"

# Add missing index on profiles table
echo ""
echo "📊 Adding indexes on profiles table..."
$PSQL -c "CREATE INDEX IF NOT EXISTS idx_profile_name ON profiles(name);" 2>&1 | grep -v "already exists" || echo "  ✅ idx_profile_name created"

# Fix plans.date to NOT NULL
echo ""
echo "🔧 Fixing plans.date constraint..."
$PSQL -c "UPDATE plans SET date = '2025-01-01' WHERE date IS NULL;" 2>&1
$PSQL -c "ALTER TABLE plans ALTER COLUMN date SET NOT NULL;" 2>&1 && echo "  ✅ plans.date is now NOT NULL"

# Fix profiles.name to NOT NULL
echo ""
echo "🔧 Fixing profiles.name constraint..."
$PSQL -c "UPDATE profiles SET name = 'Unnamed' WHERE name IS NULL OR name = '';" 2>&1
$PSQL -c "ALTER TABLE profiles ALTER COLUMN name SET NOT NULL;" 2>&1 && echo "  ✅ profiles.name is now NOT NULL"

# Add unique constraint on plan_songs (if doesn't exist)
echo ""
echo "🔧 Adding unique constraint on plan_songs..."
$PSQL -c "ALTER TABLE plan_songs ADD CONSTRAINT uq_plan_song UNIQUE (plan_id, song_id);" 2>&1 | grep -v "already exists" && echo "  ✅ uq_plan_song constraint created" || echo "  ✅ constraint already exists"

# Add order index on plan_songs
echo ""
echo "📊 Adding order index on plan_songs..."
$PSQL -c "CREATE INDEX IF NOT EXISTS idx_plan_songs_order ON plan_songs(plan_id, order_index);" 2>&1 | grep -v "already exists" || echo "  ✅ idx_plan_songs_order created"

echo ""
echo "============================================================"
echo "✅ DATABASE SCHEMA FIX COMPLETE!"
echo "============================================================"
echo ""
echo "Summary of changes:"
echo "  • Added indexes on songs (title, artist, band)"
echo "  • Added indexes on plans (date, profile_id)"
echo "  • Added index on profiles (name)"
echo "  • Fixed plans.date to NOT NULL"
echo "  • Fixed profiles.name to NOT NULL"
echo "  • Added unique constraint on plan_songs"
echo "  • Added order index on plan_songs"
echo ""

# Verify schema
echo "Verifying changes..."
$PSQL -c "\d+ songs" | head -20
$PSQL -c "\d+ plans" | head -20
$PSQL -c "\d+ plan_songs" | head -20
