#!/bin/bash
# Get SSL certificate after DNS is configured

DOMAIN="quickbooksposhelp.access.ly"

echo "Checking DNS resolution..."
if ! host "$DOMAIN" > /dev/null 2>&1; then
    echo "❌ DNS not resolving yet for $DOMAIN"
    echo ""
    echo "Please configure DNS first:"
    echo "  Host: quickbooksposhelp"
    echo "  Type: A"
    echo "  Value: 192.168.10.130"
    echo ""
    echo "Then wait 5-30 minutes for DNS propagation"
    exit 1
fi

echo "✅ DNS is resolving"
echo ""
echo "Getting SSL certificate..."

# Ensure firewall is open
sudo ufw allow 80/tcp comment "HTTP for SSL verification"
sudo ufw allow 443/tcp comment "HTTPS"

# Get certificate using certbot
sudo certbot --nginx -d "$DOMAIN" --non-interactive --agree-tos --email admin@prompttech.com --redirect

if [ $? -eq 0 ]; then
    echo ""
    echo "✅ SSL certificate installed successfully!"
    echo ""
    echo "Your site is now available at:"
    echo "  🔒 https://$DOMAIN"
    echo ""
    echo "Certificate will auto-renew before expiration"
else
    echo ""
    echo "❌ Failed to get SSL certificate"
    echo "Check the error messages above"
fi
