#!/bin/bash

# Check if the postgres service is running
docker compose ps postgres | grep "Up" > /dev/null
if [ $? -ne 0 ]; then
  echo "The postgres service is not running. Please start it with 'docker compose up -d' and try again."
  exit 1
fi

# Define database names and user
DATABASES=("whatsapp" "telegram" "discord")
USER="synapse"

# Create databases and grant permissions
for DB in "${DATABASES[@]}"; do
  echo "Creating database $DB..."
  docker compose exec -T postgres psql -U "$USER" -c "CREATE DATABASE $DB;"
  docker compose exec -T postgres psql -U "$USER" -c "GRANT ALL PRIVILEGES ON DATABASE $DB TO $USER;"
  echo "Database $DB created and permissions granted."
done

echo "All databases created successfully."