#!/bin/bash
# Script simples para corrigir establishments na VPS

cd /var/www/maktubia-backend

echo "🔧 Adicionando colunas created_at e updated_at..."
sudo -u postgres psql -d maktubia_points -c "ALTER TABLE establishments ADD COLUMN IF NOT EXISTS created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP;"
sudo -u postgres psql -d maktubia_points -c "ALTER TABLE establishments ADD COLUMN IF NOT EXISTS updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP;"

echo ""
echo "🏪 Criando estabelecimentos..."
sudo -u postgres psql -d maktubia_points << 'SQL'
INSERT INTO establishments (est_id, name, type, qr_code, is_active)
VALUES 
    ('MAKTUBIA_SHOP_001', 'Maktubia Shop Principal', 'Loja', 'MAKTUBIA_SHOP_001', TRUE),
    ('MAKTUBIA_SHOP_002', 'Maktubia Shop Centro', 'Loja', 'MAKTUBIA_SHOP_002', TRUE),
    ('MAKTUBIA_REST_001', 'Maktubia Restaurant', 'Restaurante', 'MAKTUBIA_REST_001', TRUE),
    ('MAKTUBIA_CAFE_001', 'Maktubia Café', 'Café', 'MAKTUBIA_CAFE_001', TRUE),
    ('MAKTUBIA_SUPER_001', 'Maktubia Supermercado', 'Supermercado', 'MAKTUBIA_SUPER_001', TRUE)
ON CONFLICT (est_id) DO UPDATE SET
    name = EXCLUDED.name,
    type = EXCLUDED.type,
    is_active = EXCLUDED.is_active;
SQL

echo ""
echo "✅ Verificando estabelecimentos criados..."
sudo -u postgres psql -d maktubia_points -c "SELECT est_id, name, type, qr_code, is_active FROM establishments ORDER BY est_id;"

echo ""
echo "🔄 Reiniciando aplicação..."
pm2 restart maktubia-backend

echo ""
echo "✅ Concluído!"













