#!/bin/bash

# Docker persistence test script for Queue North Website
# Verifies SQLite database survives container restart with volume mount

set -e

echo "=== Docker Persistence Test for Queue North Website ==="

# Stop any existing container
docker stop queuenorth-test 2>/dev/null || true
docker rm queuenorth-test 2>/dev/null || true

# Remove any existing volumes for fresh start
docker volume rm queuenorth-test-db 2>/dev/null || true
docker volume rm queuenorth-test-logs 2>/dev/null || true

# Build fresh image
echo "Building fresh Docker image..."
docker build -t queuenorth-test .

# Create volumes for persistence
docker volume create queuenorth-test-db > /dev/null
docker volume create queuenorth-test-logs > /dev/null

# Run container with volume mount
echo "Starting container with persistent volume..."
docker run -d \
  --name queuenorth-test \
  -p 3002:3001 \
  -v queuenorth-test-db:/app/db \
  -v queuenorth-test-logs:/app/logs \
  -e NODE_ENV=production \
  -e SERVER_PORT=3001 \
  queuenorth-test

# Wait for server to be ready
echo "Waiting for server to be ready..."
sleep 5

# Verify health endpoint
if curl -s http://localhost:3002/api/health | grep -q '"status":"ok"'; then
  echo "✓ Health check passed"
else
  echo "✗ Health check failed"
  docker logs queuenorth-test
  exit 1
fi

# Insert test data via API
echo "Inserting test lead data..."
curl -s -X POST http://localhost:3002/api/leads \
  -H "Content-Type: application/json" \
  -d '{"company":"Docker Test Co","name":"Docker Test User","email":"docker@test.com","phone":"555-DOCKER","zip":"54321","message":"Docker persistence test","service_interest":"contact-center"}' > /dev/null

echo "Inserting test support request data..."
curl -s -X POST http://localhost:3002/api/support \
  -H "Content-Type: application/json" \
  -d '{"name":"Docker Test User","company":"Docker Test Co","email":"docker@test.com","phone":"555-DOCKER","issue":"Docker persistence test support request","priority":"medium"}' > /dev/null

# Verify data was inserted using Node.js (sqlite3 CLI not available in container)
echo "Verifying data in database..."

# Check leads table
if docker exec queuenorth-test node -e "
  const sqlite3 = require('better-sqlite3');
  const db = new sqlite3('/app/db/queuenorth.db');
  const row = db.prepare('SELECT COUNT(*) as c FROM leads WHERE email=?').get('docker@test.com');
  console.log(row.c);
  process.exit(row.c === 1 ? 0 : 1);
" 2>/dev/null; then
  echo "✓ Test lead data persisted in database"
else
  echo "✗ Test lead data NOT persisted in database"
  exit 1
fi

# Check support_requests table
if docker exec queuenorth-test node -e "
  const sqlite3 = require('better-sqlite3');
  const db = new sqlite3('/app/db/queuenorth.db');
  const row = db.prepare('SELECT COUNT(*) as c FROM support_requests WHERE email=?').get('docker@test.com');
  console.log(row.c);
  process.exit(row.c === 1 ? 0 : 1);
" 2>/dev/null; then
  echo "✓ Test support request data persisted in database"
else
  echo "✗ Test support request data NOT persisted in database"
  exit 1
fi

# Stop the container (simulates restart)
echo "Stopping container to simulate restart..."
docker stop queuenorth-test > /dev/null
sleep 2

# Restart with same volume mount
echo "Restarting container with same volume..."
docker start queuenorth-test > /dev/null
sleep 3

# Verify data is still there after restart
echo "Verifying data persists after restart..."

# Check leads after restart
if docker exec queuenorth-test node -e "
  const sqlite3 = require('better-sqlite3');
  const db = new sqlite3('/app/db/queuenorth.db');
  const row = db.prepare('SELECT COUNT(*) as c FROM leads WHERE email=?').get('docker@test.com');
  console.log(row.c);
  process.exit(row.c === 1 ? 0 : 1);
" 2>/dev/null; then
  echo "✓ Data persists after container restart"
else
  echo "✗ Data NOT persisted after container restart"
  exit 1
fi

# Check support_requests after restart
if docker exec queuenorth-test node -e "
  const sqlite3 = require('better-sqlite3');
  const db = new sqlite3('/app/db/queuenorth.db');
  const row = db.prepare('SELECT COUNT(*) as c FROM support_requests WHERE email=?').get('docker@test.com');
  console.log(row.c);
  process.exit(row.c === 1 ? 0 : 1);
" 2>/dev/null; then
  echo "✓ Support requests data persists after container restart"
else
  echo "✗ Support requests data NOT persisted after container restart"
  exit 1
fi

# Cleanup
echo "Cleaning up..."
docker stop queuenorth-test > /dev/null
docker rm queuenorth-test > /dev/null
docker volume rm queuenorth-test-db > /dev/null
docker volume rm queuenorth-test-logs > /dev/null

echo ""
echo "=== All Docker persistence tests passed! ==="
echo "SQLite database correctly persists across container restarts with volume mount."
