#!/bin/bash

# Prisma Auth API Testing Script with Hurl
# This script provides easy commands to run API tests

set -e

show_help() {
    echo "Prisma Auth API Testing with Hurl"
    echo ""
    echo "Usage: $0 [COMMAND]"
    echo ""
    echo "Commands:"
    echo "  start       Start the example backend application"
    echo "  test        Run all Hurl tests"
    echo "  test-auth   Run authentication tests only"
    echo "  test-users  Run user management tests only"
    echo "  stop        Stop the application"
    echo "  logs        Show application logs"
    echo "  help        Show this help message"
    echo ""
    echo "Examples:"
    echo "  $0 start && $0 test    # Start app and run all tests"
    echo "  $0 test-auth          # Run only auth tests"
}

start_app() {
    echo "Starting Prisma Auth example backend..."
    cd example
    docker compose up -d --build
    echo "Waiting for application to be ready..."

    # Wait for the application to start
    echo "Checking application health..."
    max_attempts=30
    attempt=1

    while [ $attempt -le $max_attempts ]; do
        if curl -s -f http://localhost:3000/ >/dev/null 2>&1; then
            echo "✅ Application is ready!"
            cd ..
            return 0
        fi
        echo "Attempt $attempt/$max_attempts - waiting for application to be ready..."
        sleep 2
        attempt=$((attempt + 1))
    done

    echo "❌ Application failed to start within expected time"
    echo "Check logs with: $0 logs"
    cd ..
    exit 1
}

run_hurl_test() {
    local test_file="$1"
    local test_name="$2"

    echo "Running $test_name tests..."
    cd tests
    if hurl --test "$test_file"; then
        echo "✅ $test_name tests passed"
        cd ..
    else
        echo "❌ $test_name tests failed"
        cd ..
        return 1
    fi
}

run_all_tests() {
    echo "Running all API tests..."
    cd tests

    # Run each test file individually
    for test_file in auth.hurl users.hurl; do
        echo "Running $test_file..."
        if hurl --test "$test_file"; then
            echo "✅ $test_file passed"
        else
            echo "❌ $test_file failed"
            cd ..
            return 1
        fi
    done
    echo "✅ All tests passed"
    cd ..
}

check_dependencies() {
    # Check if hurl is installed
    if ! command -v hurl &> /dev/null; then
        echo "❌ Hurl is not installed. Please install it first:"
        echo "   Visit: https://hurl.dev/docs/installation.html"
        echo "   Or use: curl --location --remote-name https://github.com/Orange-OpenSource/hurl/releases/latest/download/hurl-{version}-x86_64-unknown-linux-gnu.tar.gz"
        exit 1
    fi

    # Check if docker is installed
    if ! command -v docker &> /dev/null; then
        echo "❌ Docker is not installed. Please install Docker first."
        exit 1
    fi
}

case "${1:-help}" in
    start)
        check_dependencies
        start_app
        ;;
    test)
        check_dependencies
        run_all_tests
        ;;
    test-auth)
        check_dependencies
        run_hurl_test "auth.hurl" "Authentication"
        ;;
    test-users)
        check_dependencies
        run_hurl_test "users.hurl" "User Management"
        ;;
    stop)
        echo "Stopping Prisma Auth application..."
        cd example
        docker compose down
        cd ..
        ;;
    logs)
        cd example
        docker compose logs -f
        cd ..
        ;;
    help|--help|-h)
        show_help
        ;;
    *)
        echo "Unknown command: $1"
        echo ""
        show_help
        exit 1
        ;;
esac