#!/bin/bash

# API Endpoint Testing Script for sol.aibitsoft.cloud (HTTPS)
# Test Date: $(date)

BASE_URL="https://sol.aibitsoft.cloud"
REPORT_FILE="api-test-report-https-$(date +%Y%m%d-%H%M%S).txt"

echo "================================================" > $REPORT_FILE
echo "API ENDPOINT TEST REPORT (HTTPS)" >> $REPORT_FILE
echo "Server: sol.aibitsoft.cloud" >> $REPORT_FILE
echo "Protocol: HTTPS" >> $REPORT_FILE
echo "Test Date: $(date)" >> $REPORT_FILE
echo "================================================" >> $REPORT_FILE
echo "" >> $REPORT_FILE

test_endpoint() {
    local method=$1
    local endpoint=$2
    local description=$3
    local data=$4
    
    echo "Testing: $method $endpoint - $description"
    
    if [ "$method" == "GET" ]; then
        response=$(curl -s -w "\nHTTP_CODE:%{http_code}\nTIME:%{time_total}" -k "$BASE_URL$endpoint" 2>&1)
    elif [ "$method" == "POST" ]; then
        if [ -z "$data" ]; then
            response=$(curl -s -w "\nHTTP_CODE:%{http_code}\nTIME:%{time_total}" -X POST -H "Content-Type: application/json" -k "$BASE_URL$endpoint" 2>&1)
        else
            response=$(curl -s -w "\nHTTP_CODE:%{http_code}\nTIME:%{time_total}" -X POST -H "Content-Type: application/json" -d "$data" -k "$BASE_URL$endpoint" 2>&1)
        fi
    elif [ "$method" == "PUT" ]; then
        if [ -z "$data" ]; then
            response=$(curl -s -w "\nHTTP_CODE:%{http_code}\nTIME:%{time_total}" -X PUT -H "Content-Type: application/json" -k "$BASE_URL$endpoint" 2>&1)
        else
            response=$(curl -s -w "\nHTTP_CODE:%{http_code}\nTIME:%{time_total}" -X PUT -H "Content-Type: application/json" -d "$data" -k "$BASE_URL$endpoint" 2>&1)
        fi
    elif [ "$method" == "DELETE" ]; then
        response=$(curl -s -w "\nHTTP_CODE:%{http_code}\nTIME:%{time_total}" -X DELETE -k "$BASE_URL$endpoint" 2>&1)
    fi
    
    http_code=$(echo "$response" | grep "HTTP_CODE" | cut -d: -f2)
    time_total=$(echo "$response" | grep "TIME" | cut -d: -f2)
    body=$(echo "$response" | sed '/HTTP_CODE/d' | sed '/TIME/d')
    
    # Status indicator
    if [ "$http_code" -ge 200 ] && [ "$http_code" -lt 300 ]; then
        status="✓ PASS"
    elif [ "$http_code" -ge 400 ] && [ "$http_code" -lt 500 ]; then
        status="⚠ EXPECTED (Client Error)"
    elif [ "$http_code" -ge 500 ]; then
        status="✗ FAIL"
    else
        status="? UNKNOWN"
    fi
    
    echo "----------------------------------------" >> $REPORT_FILE
    echo "Endpoint: $method $endpoint" >> $REPORT_FILE
    echo "Description: $description" >> $REPORT_FILE
    echo "Status: $status" >> $REPORT_FILE
    echo "HTTP Code: $http_code" >> $REPORT_FILE
    echo "Response Time: ${time_total}s" >> $REPORT_FILE
    echo "Response Body (first 500 chars):" >> $REPORT_FILE
    echo "$body" | head -c 500 >> $REPORT_FILE
    echo "" >> $REPORT_FILE
    echo "" >> $REPORT_FILE
    
    echo "  Status: $status | Code: $http_code | Time: ${time_total}s"
}

echo "Starting HTTPS API endpoint tests..."
echo ""

# ============================================
# JOBS ENDPOINTS
# ============================================
echo ">> Testing JOBS Endpoints"
echo "" >> $REPORT_FILE
echo "=== JOBS ENDPOINTS ===" >> $REPORT_FILE
echo "" >> $REPORT_FILE

test_endpoint "GET" "/api/jobs" "List all jobs"
test_endpoint "GET" "/api/jobs/invalid-id" "Get job by ID (invalid)"
test_endpoint "POST" "/api/jobs" "Create job" '{"title":"Test Job","description":"Test"}'
test_endpoint "PUT" "/api/jobs/test-id" "Update job" '{"title":"Updated Job"}'
test_endpoint "DELETE" "/api/jobs/test-id" "Delete job"

echo ""

# ============================================
# APPLICATIONS ENDPOINTS
# ============================================
echo ">> Testing APPLICATIONS Endpoints"
echo "" >> $REPORT_FILE
echo "=== APPLICATIONS ENDPOINTS ===" >> $REPORT_FILE
echo "" >> $REPORT_FILE

test_endpoint "GET" "/api/applications" "List all applications"
test_endpoint "GET" "/api/applications/invalid-id" "Get application by ID"
test_endpoint "POST" "/api/applications" "Create application" '{"jobId":"test","name":"Test User"}'
test_endpoint "PUT" "/api/applications/test-id" "Update application" '{"status":"reviewed"}'
test_endpoint "DELETE" "/api/applications/test-id" "Delete application"

echo ""

# ============================================
# USER RESUMES ENDPOINTS
# ============================================
echo ">> Testing USER RESUMES Endpoints"
echo "" >> $REPORT_FILE
echo "=== USER RESUMES ENDPOINTS ===" >> $REPORT_FILE
echo "" >> $REPORT_FILE

test_endpoint "GET" "/api/user-resumes" "List all user resumes"
test_endpoint "POST" "/api/user-resumes" "Create user resume" '{"userId":"test","resumeData":"test"}'

echo ""

# ============================================
# TEAM MEMBERS ENDPOINTS
# ============================================
echo ">> Testing TEAM MEMBERS Endpoints"
echo "" >> $REPORT_FILE
echo "=== TEAM MEMBERS ENDPOINTS ===" >> $REPORT_FILE
echo "" >> $REPORT_FILE

test_endpoint "GET" "/api/team-members" "List all team members"
test_endpoint "GET" "/api/team-members/invalid-id" "Get team member by ID"
test_endpoint "POST" "/api/team-members" "Create team member" '{"name":"Test Member","role":"Developer"}'
test_endpoint "POST" "/api/team-members/seed" "Seed team members"
test_endpoint "PUT" "/api/team-members/test-id" "Update team member" '{"role":"Senior Developer"}'
test_endpoint "DELETE" "/api/team-members/test-id" "Delete team member"

echo ""

# ============================================
# BLOGS ENDPOINTS
# ============================================
echo ">> Testing BLOGS Endpoints"
echo "" >> $REPORT_FILE
echo "=== BLOGS ENDPOINTS ===" >> $REPORT_FILE
echo "" >> $REPORT_FILE

test_endpoint "GET" "/api/blogs" "List all blogs"
test_endpoint "GET" "/api/blogs/category/tech" "Get blogs by category"
test_endpoint "GET" "/api/blogs/category/Blogs-IT-Staff" "Get blogs by category (existing)"
test_endpoint "GET" "/api/blogs/invalid-id" "Get blog by ID"
test_endpoint "POST" "/api/blogs" "Create blog" '{"title":"Test Blog","content":"Content"}'
test_endpoint "PUT" "/api/blogs/test-id" "Update blog" '{"title":"Updated Blog"}'
test_endpoint "PUT" "/api/blogs/category/tech/hero" "Update hero section"
test_endpoint "PUT" "/api/blogs/category/tech/component/test-component-id" "Update component"
test_endpoint "PUT" "/api/blogs/category/tech" "Update blog by category"
test_endpoint "DELETE" "/api/blogs/test-id" "Delete blog"

echo ""

# ============================================
# BLOG STATIC DATA ENDPOINTS
# ============================================
echo ">> Testing BLOG STATIC DATA Endpoints"
echo "" >> $REPORT_FILE
echo "=== BLOG STATIC DATA ENDPOINTS ===" >> $REPORT_FILE
echo "" >> $REPORT_FILE

test_endpoint "GET" "/api/blog-static-data/tech" "Get static data by category"
test_endpoint "GET" "/api/blog-static-data/Blogs-IT-Staff" "Get static data by category (existing)"

echo ""

# ============================================
# ZOOM MEETING ENDPOINTS
# ============================================
echo ">> Testing ZOOM MEETING Endpoints"
echo "" >> $REPORT_FILE
echo "=== ZOOM MEETING ENDPOINTS ===" >> $REPORT_FILE
echo "" >> $REPORT_FILE

test_endpoint "GET" "/api/" "List all zoom meetings (root)"
test_endpoint "GET" "/api/invalid-id" "Get zoom meeting by ID"
test_endpoint "GET" "/api/zoom/invalid-zoom-id" "Get zoom meeting by Zoom ID"
test_endpoint "POST" "/api/schedule-zoom" "Schedule zoom meeting" '{"fullName":"Test","email":"test@test.com"}'
test_endpoint "POST" "/api/create-meeting" "Create zoom meeting" '{"fullName":"Test","email":"test@test.com"}'
test_endpoint "POST" "/api/" "Create zoom meeting (root)" '{"fullName":"Test","email":"test@test.com"}'
test_endpoint "PUT" "/api/test-id/cancel" "Cancel zoom meeting"
test_endpoint "PUT" "/api/test-id" "Update zoom meeting" '{"status":"cancelled"}'
test_endpoint "DELETE" "/api/test-id" "Delete zoom meeting"

echo ""

# ============================================
# GOOGLE MEET ENDPOINTS
# ============================================
echo ">> Testing GOOGLE MEET Endpoints"
echo "" >> $REPORT_FILE
echo "=== GOOGLE MEET ENDPOINTS ===" >> $REPORT_FILE
echo "" >> $REPORT_FILE

test_endpoint "GET" "/api/google-meet" "List all Google Meet meetings"
test_endpoint "GET" "/api/google-meet/invalid-id" "Get Google Meet by ID"
test_endpoint "GET" "/api/google-meet/event/invalid-event-id" "Get Google Meet by Event ID"
test_endpoint "POST" "/api/google-meet/schedule-google-meet" "Schedule Google Meet" '{"fullName":"Test","email":"test@test.com"}'
test_endpoint "POST" "/api/google-meet/create-google-meet" "Create Google Meet" '{"fullName":"Test","email":"test@test.com"}'
test_endpoint "POST" "/api/google-meet" "Create Google Meet (root)" '{"fullName":"Test","email":"test@test.com"}'
test_endpoint "PUT" "/api/google-meet/test-id/cancel" "Cancel Google Meet"
test_endpoint "PUT" "/api/google-meet/test-id" "Update Google Meet" '{"status":"cancelled"}'
test_endpoint "DELETE" "/api/google-meet/test-id" "Delete Google Meet"

echo ""

# ============================================
# TEMPLATES ENDPOINTS
# ============================================
echo ">> Testing TEMPLATES Endpoints"
echo "" >> $REPORT_FILE
echo "=== TEMPLATES ENDPOINTS ===" >> $REPORT_FILE
echo "" >> $REPORT_FILE

test_endpoint "GET" "/api/templates" "List all templates"
test_endpoint "GET" "/api/templates/invalid-id" "Get template by ID"
test_endpoint "POST" "/api/templates" "Create template" '{"name":"Test Template","type":"resume"}'
test_endpoint "DELETE" "/api/templates/test-id" "Delete template"

echo ""

# ============================================
# INTERVIEW EMAIL ENDPOINTS
# ============================================
echo ">> Testing INTERVIEW EMAIL Endpoints"
echo "" >> $REPORT_FILE
echo "=== INTERVIEW EMAIL ENDPOINTS ===" >> $REPORT_FILE
echo "" >> $REPORT_FILE

test_endpoint "POST" "/api/interviews/send-email" "Send interview email" '{"to":"test@test.com","subject":"Interview"}'

echo ""

# ============================================
# CONTACT FORM ENDPOINTS
# ============================================
echo ">> Testing CONTACT FORM Endpoints"
echo "" >> $REPORT_FILE
echo "=== CONTACT FORM ENDPOINTS ===" >> $REPORT_FILE
echo "" >> $REPORT_FILE

test_endpoint "POST" "/api/contact" "Submit contact form" '{"name":"Test User","email":"test@test.com","message":"Test message"}'

echo ""

# ============================================
# PROPOSAL FORM ENDPOINTS
# ============================================
echo ">> Testing PROPOSAL FORM Endpoints"
echo "" >> $REPORT_FILE
echo "=== PROPOSAL FORM ENDPOINTS ===" >> $REPORT_FILE
echo "" >> $REPORT_FILE

test_endpoint "POST" "/api/proposal" "Submit proposal form" '{"company":"Test Co","email":"test@test.com"}'

echo ""

# ============================================
# BOOK EXPERT FORM ENDPOINTS
# ============================================
echo ">> Testing BOOK EXPERT FORM Endpoints"
echo "" >> $REPORT_FILE
echo "=== BOOK EXPERT FORM ENDPOINTS ===" >> $REPORT_FILE
echo "" >> $REPORT_FILE

test_endpoint "POST" "/api/book-expert" "Submit book expert form" '{"name":"Test","email":"test@test.com"}'

echo ""

# ============================================
# PARTNER FORM ENDPOINTS
# ============================================
echo ">> Testing PARTNER FORM Endpoints"
echo "" >> $REPORT_FILE
echo "=== PARTNER FORM ENDPOINTS ===" >> $REPORT_FILE
echo "" >> $REPORT_FILE

test_endpoint "POST" "/api/partner" "Submit partner form" '{"company":"Test Co","email":"test@test.com"}'

echo ""

# ============================================
# USER ENDPOINTS
# ============================================
echo ">> Testing USER Endpoints"
echo "" >> $REPORT_FILE
echo "=== USER ENDPOINTS ===" >> $REPORT_FILE
echo "" >> $REPORT_FILE

test_endpoint "POST" "/api/users/login" "Save user login" '{"email":"test@test.com","provider":"google"}'
test_endpoint "GET" "/api/users/email/test@test.com" "Get user by email"
test_endpoint "GET" "/api/users/email/aibitsofts@gmail.com" "Get user by email (existing)"
test_endpoint "GET" "/api/users/me" "Get current user"

echo ""

# ============================================
# AUTH ENDPOINTS
# ============================================
echo ">> Testing AUTH Endpoints"
echo "" >> $REPORT_FILE
echo "=== AUTH ENDPOINTS ===" >> $REPORT_FILE
echo "" >> $REPORT_FILE

test_endpoint "GET" "/auth/google/debug" "Debug OAuth configuration"
test_endpoint "GET" "/auth/google" "Initiate Google OAuth (redirects)"
test_endpoint "GET" "/auth/google/callback?code=test" "Google OAuth callback"

echo ""

# ============================================
# ROOT ENDPOINT
# ============================================
echo ">> Testing ROOT Endpoint"
echo "" >> $REPORT_FILE
echo "=== ROOT ENDPOINT ===" >> $REPORT_FILE
echo "" >> $REPORT_FILE

test_endpoint "GET" "/api" "Root API endpoint"

echo ""

# ============================================
# ADDITIONAL EDGE CASES
# ============================================
echo ">> Testing Additional Edge Cases"
echo "" >> $REPORT_FILE
echo "=== ADDITIONAL EDGE CASES ===" >> $REPORT_FILE
echo "" >> $REPORT_FILE

test_endpoint "GET" "/" "Root path"
test_endpoint "GET" "/api/" "API root with trailing slash"
test_endpoint "GET" "/api/jobs/" "Jobs with trailing slash"
test_endpoint "GET" "/api/invalid-route" "Invalid route"
test_endpoint "GET" "/.well-known/acme-challenge/test" "ACME challenge path"

echo ""

echo "================================================" >> $REPORT_FILE
echo "TEST SUMMARY" >> $REPORT_FILE
echo "================================================" >> $REPORT_FILE
echo "Total Endpoints Tested: $(grep -c "Endpoint:" $REPORT_FILE)" >> $REPORT_FILE
echo "Report saved to: $REPORT_FILE" >> $REPORT_FILE
echo "================================================" >> $REPORT_FILE

echo ""
echo "================================================"
echo "HTTPS Testing Complete!"
echo "Report saved to: $REPORT_FILE"
echo "================================================"

