#!/bin/bash
set -e

# PostgreSQL initialization script for compose core stack
# This script runs on first database initialization
# Creates all databases required by compose.sh stacks

echo "Starting compose database initialization..."

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
    -- Create databases for compose services
    -- Main application database
    SELECT 'CREATE DATABASE directus'
    WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'directus')\gexec

    -- Umami analytics database
    SELECT 'CREATE DATABASE umami'
    WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'umami')\gexec

    -- n8n workflow automation database
    SELECT 'CREATE DATABASE n8n'
    WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'n8n')\gexec

    -- Linkwarden bookmark manager database
    SELECT 'CREATE DATABASE linkwarden'
    WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'linkwarden')\gexec

    -- Joplin note-taking server database
    SELECT 'CREATE DATABASE joplin'
    WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'joplin')\gexec

    -- Mattermost chat platform database
    SELECT 'CREATE DATABASE mattermost'
    WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'mattermost')\gexec

    -- Tandoor recipe manager database
    SELECT 'CREATE DATABASE tandoor'
    WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'tandoor')\gexec

    -- Asciinema terminal recording server database
    SELECT 'CREATE DATABASE asciinema'
    WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'asciinema')\gexec

    -- Grant privileges to all databases
    GRANT ALL PRIVILEGES ON DATABASE directus TO $POSTGRES_USER;
    GRANT ALL PRIVILEGES ON DATABASE umami TO $POSTGRES_USER;
    GRANT ALL PRIVILEGES ON DATABASE n8n TO $POSTGRES_USER;
    GRANT ALL PRIVILEGES ON DATABASE linkwarden TO $POSTGRES_USER;
    GRANT ALL PRIVILEGES ON DATABASE joplin TO $POSTGRES_USER;
    GRANT ALL PRIVILEGES ON DATABASE mattermost TO $POSTGRES_USER;
    GRANT ALL PRIVILEGES ON DATABASE tandoor TO $POSTGRES_USER;
    GRANT ALL PRIVILEGES ON DATABASE asciinema TO $POSTGRES_USER;

    -- Log success
    SELECT 'Compose databases initialized:' AS status;
    SELECT datname FROM pg_database
    WHERE datname IN ('directus', 'umami', 'n8n', 'linkwarden', 'joplin', 'mattermost', 'tandoor', 'asciinema')
    ORDER BY datname;
EOSQL

echo ""
echo "✓ PostgreSQL initialization completed"
echo "✓ All compose databases created successfully"
echo ""
echo "Databases available:"
echo "  • directus     - Sexy application database"
echo "  • umami        - Tracking database"
echo "  • n8n          - Workflow automation database"
echo "  • linkwarden   - Bookmark manager database"
echo "  • joplin       - Note-taking server database"
echo "  • mattermost   - Chat platform database"
echo "  • tandoor      - Recipe manager database"
echo "  • asciinema    - Terminal recording server database"
echo ""
