#!/bin/bash

# Notification System Setup Script
# This script sets up the queue-based notification system

echo "=========================================="
echo "Revenue Assurance - Notification Setup"
echo "=========================================="
echo ""

# Check if we're in the backend_api directory
if [ ! -f "artisan" ]; then
    echo "Error: Please run this script from the backend_api directory"
    exit 1
fi

# Step 1: Run migrations
echo "Step 1: Running database migrations..."
php artisan migrate --force
if [ $? -eq 0 ]; then
    echo "✓ Migrations completed successfully"
else
    echo "✗ Migration failed"
    exit 1
fi
echo ""

# Step 2: Check queue configuration
echo "Step 2: Checking queue configuration..."
QUEUE_CONNECTION=$(php artisan tinker --execute="echo config('queue.default');" 2>/dev/null | tail -n 1)
echo "Current queue connection: $QUEUE_CONNECTION"
if [ "$QUEUE_CONNECTION" != "database" ]; then
    echo "⚠ Warning: Queue connection is not set to 'database'"
    echo "  Please update QUEUE_CONNECTION=database in your .env file"
fi
echo ""

# Step 3: Check mail configuration
echo "Step 3: Checking mail configuration..."
MAIL_MAILER=$(grep "^MAIL_MAILER=" .env | cut -d '=' -f2)
if [ -z "$MAIL_MAILER" ]; then
    echo "⚠ Warning: MAIL_MAILER not configured in .env"
    echo "  Please configure mail settings in .env file"
else
    echo "Mail mailer: $MAIL_MAILER"
fi
echo ""

# Step 4: Test queue worker
echo "Step 4: Testing queue worker..."
echo "Starting queue worker for 5 seconds..."
timeout 5 php artisan queue:work --queue=notifications --stop-when-empty 2>&1 | head -n 5 &
WORKER_PID=$!
sleep 2
if ps -p $WORKER_PID > /dev/null 2>&1; then
    echo "✓ Queue worker started successfully"
    kill $WORKER_PID 2>/dev/null
else
    echo "✓ Queue worker test completed"
fi
echo ""

# Step 5: Display queue status
echo "Step 5: Queue status..."
php artisan queue:monitor 2>/dev/null || echo "No jobs in queue"
echo ""

# Step 6: Check for failed jobs
echo "Step 6: Checking for failed jobs..."
FAILED_COUNT=$(php artisan queue:failed 2>/dev/null | grep -c "ID" || echo "0")
if [ "$FAILED_COUNT" -gt 0 ]; then
    echo "⚠ Warning: $FAILED_COUNT failed jobs found"
    echo "  Run 'php artisan queue:failed' to view them"
    echo "  Run 'php artisan queue:retry all' to retry them"
else
    echo "✓ No failed jobs"
fi
echo ""

# Final instructions
echo "=========================================="
echo "Setup Complete!"
echo "=========================================="
echo ""
echo "Next Steps:"
echo "1. Ensure your .env file has correct mail configuration"
echo "2. Start the queue worker:"
echo "   php artisan queue:work --queue=notifications"
echo ""
echo "For production, use a process manager like Supervisor"
echo "See NOTIFICATION_SYSTEM_IMPLEMENTATION.md for details"
echo ""
echo "To test notifications:"
echo "1. Create a new entry with an email address"
echo "2. Check the queue: php artisan queue:monitor"
echo "3. View logs: tail -f storage/logs/laravel.log"
echo ""
