#!/bin/bash
# Monaco USA Portal - Set Role Passwords
# This script runs AFTER init.sql to set passwords for Supabase roles
# using the POSTGRES_PASSWORD environment variable.
#
# The Supabase postgres image's internal migrate.sh expects supabase_admin
# to have a password matching POSTGRES_PASSWORD.

set -e

echo "Setting passwords for Supabase roles..."

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
    -- Set supabase_admin password (required by Supabase internal scripts)
    ALTER ROLE supabase_admin WITH PASSWORD '${POSTGRES_PASSWORD}';

    -- Set authenticator password (used by PostgREST)
    ALTER ROLE authenticator WITH PASSWORD '${POSTGRES_PASSWORD}';

    -- Set supabase_auth_admin password (used by GoTrue)
    ALTER ROLE supabase_auth_admin WITH LOGIN PASSWORD '${POSTGRES_PASSWORD}';

    -- Set supabase_storage_admin password (used by Storage API)
    ALTER ROLE supabase_storage_admin WITH LOGIN PASSWORD '${POSTGRES_PASSWORD}';
EOSQL

echo "Supabase role passwords configured successfully."
