#!/bin/bash

# Test configuration persistence after container restart
# Usage: ./04_persistence_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 "=== Configuration Persistence Test ==="
echo "Using DNS server localhost:${HOST_DNS_PORT}"

# 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: Stopping current container..."

# Stop the container
docker compose down

echo "Container stopped"

echo ""
echo "Step 2: Verifying persistent configuration exists..."

# Check if modified configuration exists
DB_FILE="$TEST_DIR/private/argus/bind/db.argus.com"

if [ ! -f "$DB_FILE" ]; then
    echo "✗ Persistent configuration file not found: $DB_FILE"
    exit 1
fi

# Check if the modified IP is in the configuration
if grep -q "192.168.1.100" "$DB_FILE"; then
    echo "✓ Modified IP (192.168.1.100) found in persistent configuration"
else
    echo "✗ Modified IP not found in persistent configuration"
    echo "Configuration content:"
    cat "$DB_FILE"
    exit 1
fi

echo ""
echo "Step 3: Restarting container with persistent configuration..."

# Start the container again
docker compose up -d

echo "Waiting for container to be ready..."
sleep 5

# Check if container is running
if ! docker compose ps | grep -q "Up"; then
    echo "✗ Failed to restart container"
    docker compose logs
    exit 1
fi

echo "✓ Container restarted successfully"

echo ""
echo "Step 4: Testing DNS resolution after restart..."

# Wait a bit more for DNS to be fully ready
sleep 5

# Test that the modified configuration is still active
if ! test_dns_query "web" "192.168.1.100" "Persistent web.argus.com resolution"; then
    echo "✗ Persistent configuration 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 restart"
    exit 1
fi

echo ""
echo "Step 5: Verifying configuration files are linked correctly..."

# Check that the persistent files are properly linked
echo "Checking file links in container:"
docker compose exec bind9 ls -la /etc/bind/named.conf.local /etc/bind/db.argus.com

echo ""
echo "✓ Configuration persistence test completed successfully!"
echo "✓ Modified IP (192.168.1.100) persisted after container restart"
echo "✓ Configuration files properly linked to persistent storage"
echo "✓ DNS resolution working correctly with persisted configuration"
