#!/bin/bash

# End-to-end test for BIND9 DNS server
# This script runs all tests in sequence to validate the complete functionality
# Usage: ./00_e2e_test.sh

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
HOST_DNS_PORT="${HOST_DNS_PORT:-1053}"

export HOST_DNS_PORT

echo "=========================================="
echo "BIND9 DNS Server End-to-End Test Suite"
echo "=========================================="

# Track test results
total_tests=0
passed_tests=0
failed_tests=0

# Function to run a test step
run_test_step() {
    local step_name="$1"
    local script_name="$2"
    local description="$3"

    echo ""
    echo "[$step_name] $description"
    echo "$(printf '=%.0s' {1..50})"

    ((total_tests++))

    if [ ! -f "$SCRIPT_DIR/$script_name" ]; then
        echo "✗ Test script not found: $script_name"
        ((failed_tests++))
        return 1
    fi

    # Make sure script is executable
    chmod +x "$SCRIPT_DIR/$script_name"

    # Run the test
    echo "Executing: $SCRIPT_DIR/$script_name"
    if "$SCRIPT_DIR/$script_name"; then
        echo "✓ $step_name completed successfully"
        ((passed_tests++))
        return 0
    else
        echo "✗ $step_name failed"
        ((failed_tests++))
        return 1
    fi
}

# Cleanup any previous test environment (but preserve the Docker image)
echo ""
echo "[SETUP] Cleaning up any previous test environment..."
if [ -f "$SCRIPT_DIR/05_cleanup.sh" ]; then
    chmod +x "$SCRIPT_DIR/05_cleanup.sh"
    "$SCRIPT_DIR/05_cleanup.sh" || true
fi

echo ""
echo "Starting BIND9 DNS server end-to-end test sequence..."

# Test sequence
run_test_step "TEST-01" "01_start_container.sh" "Start BIND9 container" || true

run_test_step "TEST-02" "02_dig_test.sh" "Initial DNS resolution test" || true

run_test_step "TEST-03" "03_reload_test.sh" "Configuration reload with IP modification" || true

run_test_step "TEST-03.5" "03.5_dns_sync_test.sh" "DNS auto-sync functionality test" || true

run_test_step "TEST-04" "04_persistence_test.sh" "Configuration persistence after restart" || true

# Final cleanup (but preserve logs for review)
echo ""
echo "[CLEANUP] Cleaning up test environment..."
run_test_step "CLEANUP" "05_cleanup.sh" "Clean up containers and networks" || true

# Test summary
echo ""
echo "=========================================="
echo "TEST SUMMARY"
echo "=========================================="
echo "Total tests: $total_tests"
echo "Passed: $passed_tests"
echo "Failed: $failed_tests"

if [ $failed_tests -eq 0 ]; then
    echo ""
    echo "✅ ALL TESTS PASSED!"
    echo ""
    echo "BIND9 DNS server functionality validated:"
    echo "  ✓ Container startup and basic functionality"
    echo "  ✓ DNS resolution for configured domains"
    echo "  ✓ Configuration modification and reload"
    echo "  ✓ DNS auto-sync from IP files"
    echo "  ✓ Configuration persistence across restarts"
    echo "  ✓ Cleanup and resource management"
    echo ""
    echo "The BIND9 DNS server is ready for production use."
    exit 0
else
    echo ""
    echo "❌ SOME TESTS FAILED!"
    echo ""
    echo "Please review the test output above to identify and fix issues."
    echo "You may need to:"
    echo "  - Check Docker installation and permissions"
    echo "  - Verify network connectivity"
    echo "  - Review BIND9 configuration files"
    echo "  - Check system resources and port availability"
    exit 1
fi
