#!/bin/bash

# Production Smoke Tests
# Run these tests after bringing the site live
# Execute with: ./production_smoke_tests.sh

set -e  # Exit on any error

echo "=== PRODUCTION SMOKE TESTS ==="
echo "Started at: $(date)"
echo "Domain: https://your-domain.com"  # Update this
echo ""

DOMAIN="https://your-domain.com"  # Update this to your actual domain
SUPER_ADMIN_EMAIL="tsdcambodia@gmail.com"
SUPER_ADMIN_PASS="123456"
STORE_ADMIN_EMAIL="admin@example.com"
STORE_ADMIN_PASS="123456"

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Function to print status
print_status() {
    local status=$1
    local message=$2
    if [ "$status" = "PASS" ]; then
        echo -e "${GREEN}✓ PASS${NC}: $message"
    elif [ "$status" = "FAIL" ]; then
        echo -e "${RED}✗ FAIL${NC}: $message"
    else
        echo -e "${YELLOW}? $status${NC}: $message"
    fi
}

echo "=== BASIC HEALTH CHECKS ==="

# Test 1: HTTP response
echo "1. Testing HTTP response..."
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" "$DOMAIN")
if [ "$HTTP_CODE" = "200" ]; then
    print_status "PASS" "Site responds with HTTP 200"
else
    print_status "FAIL" "Site responds with HTTP $HTTP_CODE (expected 200)"
fi

# Test 2: Laravel health check (if endpoint exists)
echo "2. Testing Laravel application..."
LARAVEL_RESPONSE=$(curl -s "$DOMAIN" | grep -o "Laravel" | head -1)
if [ "$LARAVEL_RESPONSE" = "Laravel" ]; then
    print_status "PASS" "Laravel application is responding"
else
    print_status "WARN" "Could not verify Laravel application (may be SPA)"
fi

echo ""
echo "=== LOG VERIFICATION ==="

# Test 3: Check for recent errors in logs
echo "3. Checking application logs for errors..."
TODAY=$(date +%Y-%m-%d)
LOG_FILE="storage/logs/laravel-$TODAY.log"

if [ -f "$LOG_FILE" ]; then
    ERROR_COUNT=$(grep -c "ERROR\|CRITICAL\|EMERGENCY" "$LOG_FILE" 2>/dev/null || echo "0")
    if [ "$ERROR_COUNT" -eq 0 ]; then
        print_status "PASS" "No errors found in today's logs"
    else
        print_status "WARN" "Found $ERROR_COUNT errors in today's logs - please review"
        echo "Recent errors:"
        tail -10 "$LOG_FILE" | grep -E "ERROR|CRITICAL|EMERGENCY" || echo "No recent critical errors"
    fi
else
    print_status "WARN" "Log file not found: $LOG_FILE"
fi

echo ""
echo "=== MANUAL TESTS REQUIRED ==="
echo ""
echo "Please perform these manual tests in a browser:"
echo ""

echo "SUPER ADMIN TESTS:"
echo "1. Navigate to: $DOMAIN"
echo "2. Login with: $SUPER_ADMIN_EMAIL / $SUPER_ADMIN_PASS"
echo "3. Verify dashboard loads"
echo "4. Check user management"
echo "5. Verify restaurant/branch management"
echo ""

echo "STORE ADMIN TESTS:"
echo "1. Logout and login with: $STORE_ADMIN_EMAIL / $STORE_ADMIN_PASS"
echo "2. Navigate to POS screen"
echo "3. Verify menu items load"
echo "4. Create a test order:"
echo "   - Select items"
echo "   - Apply payment"
echo "   - Complete order"
echo "5. Check order history"
echo ""

echo "INVENTORY TESTS:"
echo "1. Navigate to Inventory → Purchase Orders"
echo "2. Create a test Purchase Order"
echo "3. Verify supplier selection works"
echo "4. Add items to PO"
echo "5. Save PO successfully"
echo ""

echo "BRANCH TYPE TESTS:"
echo "1. Switch between restaurant and retail branches"
echo "2. Verify different UI elements appear (retail: products vs restaurant: menu items)"
echo "3. Test barcode scanning (retail branches)"
echo "4. Test wholesale pricing (retail branches)"
echo ""

echo "TRANSLATION TESTS:"
echo "1. Change language to Khmer (ភាសាខ្មែរ)"
echo "2. Verify menu items are translated"
echo "3. Change to Chinese Simplified"
echo "4. Verify translations work"
echo ""

echo "ACTIVITY LOGGING TESTS:"
echo "1. Perform an action (create order, update user, etc.)"
echo "2. Check Activity Logs to ensure the action was logged"
echo "3. Verify log contains correct details"
echo ""

echo "=== COMPLETION CHECKLIST ==="
echo ""
echo "After completing manual tests, verify:"
echo "☐ All pages load without errors"
echo "☐ POS functionality works for both branch types"
echo "☐ Purchase Orders can be created and managed"
echo "☐ Translations display correctly"
echo "☐ Activity logging captures user actions"
echo "☐ No JavaScript errors in browser console"
echo "☐ Mobile responsiveness works"
echo ""

echo "=== TEST RESULTS SUMMARY ==="
echo "Completed at: $(date)"
echo ""
echo "If all tests pass, migration is successful!"
echo "If any tests fail, consider rollback using: ./production_rollback.sh"
