#!/bin/bash

# Script to set up test RSA keys for CI/CD environments
# This ensures tests run with proper keys without affecting production keys

set -e

# Define the test key directory
TEST_KEY_DIR="storage/testing/oauth"

# Function to generate test keys
generate_test_keys() {
    echo "Setting up test RSA keys..."
    
    # Create test directory if it doesn't exist
    mkdir -p "$TEST_KEY_DIR"
    
    # Generate test keys using artisan command
    php artisan app:generate-keys --path="$TEST_KEY_DIR"
    
    echo "✅ Test keys generated in $TEST_KEY_DIR"
}

# Function to clean up test keys
cleanup_test_keys() {
    echo "Cleaning up test RSA keys..."
    
    if [ -d "$TEST_KEY_DIR" ]; then
        rm -rf "$TEST_KEY_DIR"
        echo "✅ Test keys cleaned up"
    else
        echo "ℹ️  No test keys found to clean up"
    fi
}

# Main script logic
case "${1:-setup}" in
    "setup")
        generate_test_keys
        ;;
    "cleanup")
        cleanup_test_keys
        ;;
    "reset")
        cleanup_test_keys
        generate_test_keys
        ;;
    *)
        echo "Usage: $0 [setup|cleanup|reset]"
        echo "  setup   - Generate test keys (default)"
        echo "  cleanup - Remove test keys"
        echo "  reset   - Remove and regenerate test keys"
        exit 1
        ;;
esac