#!/bin/bash

# Test DNS configuration reload with IP modification
# Usage: ./03_reload_test.sh

set -e

HOST_DNS_PORT="${HOST_DNS_PORT:-1053}"

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TEST_DIR="$(dirname "$SCRIPT_DIR")"

echo "=== DNS Configuration Reload Test ==="
echo "Using DNS server localhost:${HOST_DNS_PORT}"

# Check if container is running
if ! docker compose ps | grep -q "Up"; then
    echo "Error: BIND9 container is not running"
    echo "Please start the container first with: ./01_start_container.sh"
    exit 1
fi

# Check if dig is available
if ! command -v dig &> /dev/null; then
    echo "Installing dig (dnsutils)..."
    apt-get update && apt-get install -y dnsutils
fi

# Function to test DNS query
test_dns_query() {
    local hostname="$1"
    local expected_ip="$2"
    local description="$3"

    echo "Testing: $description"
    echo "Query: $hostname.argus.com -> Expected: $expected_ip"

    result=$(dig @localhost -p "$HOST_DNS_PORT" "$hostname".argus.com A +short 2>/dev/null || echo "QUERY_FAILED")

    if [ "$result" = "$expected_ip" ]; then
        echo "✓ $result"
        return 0
    else
        echo "✗ Got: $result, Expected: $expected_ip"
        return 1
    fi
}

echo ""
echo "Step 1: Testing initial DNS configuration..."

# Test initial configuration
if ! test_dns_query "web" "12.4.5.6" "Initial web.argus.com resolution"; then
    echo "Initial DNS test failed"
    exit 1
fi

echo ""
echo "Step 2: Modifying DNS configuration..."

# Backup original configuration
cp "$TEST_DIR/private/argus/bind/db.argus.com" "$TEST_DIR/private/argus/bind/db.argus.com.backup" 2>/dev/null || true

# Create new configuration with modified IP
DB_FILE="$TEST_DIR/private/argus/bind/db.argus.com"

# Check if persistent config exists, if not use from container
if [ ! -f "$DB_FILE" ]; then
    echo "Persistent config not found, copying from container..."
    docker compose exec bind9 cp /etc/bind/db.argus.com /private/argus/bind/db.argus.com
    docker compose exec bind9 chown bind:bind /private/argus/bind/db.argus.com
fi

# Modify the IP address (12.4.5.6 -> 192.168.1.100)
sed -i 's/12\.4\.5\.6/192.168.1.100/g' "$DB_FILE"

# Increment serial number for DNS cache invalidation
current_serial=$(grep -o "2[[:space:]]*;" "$DB_FILE" | grep -o "2")
new_serial=$((current_serial + 1))
sed -i "s/2[[:space:]]*;/${new_serial}         ;/" "$DB_FILE"

echo "Modified configuration:"
echo "- Changed web.argus.com IP: 12.4.5.6 -> 192.168.1.100"
echo "- Updated serial number: $current_serial -> $new_serial"

echo ""
echo "Step 3: Reloading BIND9 configuration..."

# Reload BIND9 configuration
docker compose exec bind9 /usr/local/bin/reload-bind9.sh

echo "Configuration reloaded"

# Wait a moment for changes to take effect
sleep 3

echo ""
echo "Step 4: Testing modified DNS configuration..."

# Test modified configuration
if ! test_dns_query "web" "192.168.1.100" "Modified web.argus.com resolution"; then
    echo "Modified DNS test failed"
    exit 1
fi

# Also verify ns1 still works
if ! test_dns_query "ns1" "127.0.0.1" "ns1.argus.com still working"; then
    echo "ns1 DNS test failed after reload"
    exit 1
fi

echo ""
echo "✓ DNS configuration reload test completed successfully!"
echo "✓ IP address changed from 12.4.5.6 to 192.168.1.100"
echo "✓ Configuration persisted and reloaded correctly"
