#!/bin/bash

# Admin Initialization Script for Ginxsom Testing
# Sets up the test admin key in the database

set -e

# Test admin public key (must match TEST_ADMIN_PUBKEY from admin_test.sh)
TEST_ADMIN_PUBKEY="2ef05348f28d24e0f0ed0751278442c27b62c823c37af8d8d89d8592c6ee84e7"

echo "Initializing admin access for testing..."

# Check if database exists
if [ ! -f "db/ginxsom.db" ]; then
    echo "Error: Database db/ginxsom.db not found. Run ./db/init.sh first."
    exit 1
fi

# Configure admin settings
sqlite3 db/ginxsom.db << EOF
INSERT OR REPLACE INTO server_config (key, value, description) VALUES
    ('admin_pubkey', '$TEST_ADMIN_PUBKEY', 'Nostr public key authorized for admin operations (test key)'),
    ('admin_enabled', 'true', 'Enable admin interface');
EOF

echo "Admin access configured successfully!"
echo "Test admin public key: $TEST_ADMIN_PUBKEY"
echo "Use private key from admin_test.sh to generate authentication tokens"

# Verify configuration
echo ""
echo "Current admin configuration:"
sqlite3 db/ginxsom.db "SELECT key, value FROM server_config WHERE key IN ('admin_pubkey', 'admin_enabled');"