#!/bin/bash
set -e

# IT0 uses schema-per-tenant with a single database
# This script ensures the main database exists and creates
# required schemas for multi-tenant isolation

echo "Initializing IT0 database..."

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
    -- Create shared schema for cross-tenant data
    CREATE SCHEMA IF NOT EXISTS shared;

    -- Create default tenant schema
    CREATE SCHEMA IF NOT EXISTS tenant_default;

    -- Grant permissions
    GRANT ALL ON SCHEMA shared TO $POSTGRES_USER;
    GRANT ALL ON SCHEMA tenant_default TO $POSTGRES_USER;
EOSQL

echo "Database initialization complete!"
