#!/bin/bash
# Setup script that uses PostgreSQL superuser to create tables
# Usage: ./scripts/setup-with-superuser.sh [postgres_user] [postgres_password]

POSTGRES_USER=${1:-postgres}
POSTGRES_PASSWORD=${2:-}
DB_NAME="punimtag"

echo "Creating database tables using PostgreSQL superuser..."
echo ""

if [ -z "$POSTGRES_PASSWORD" ]; then
    # Try without password (trust authentication)
    PGPASSWORD="" psql -U "$POSTGRES_USER" -d "$DB_NAME" -f setup-auth-complete.sql
else
    # Use password
    PGPASSWORD="$POSTGRES_PASSWORD" psql -U "$POSTGRES_USER" -d "$DB_NAME" -f setup-auth-complete.sql
fi

if [ $? -eq 0 ]; then
    echo ""
    echo "✅ Tables created! Now creating admin user..."
    echo ""
    npx tsx scripts/create-admin-user.ts
else
    echo ""
    echo "❌ Failed to create tables. Please check:"
    echo "   1. PostgreSQL superuser credentials"
    echo "   2. Database name is correct: $DB_NAME"
    echo "   3. You have CREATE TABLE permissions"
fi



