#!/bin/bash

# ThrillWiki API Endpoint Testing Script
# Tests all available API endpoints with curl
# Base URL: http://127.0.0.1:8000

BASE_URL="http://127.0.0.1:8000"

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

# Function to print section headers
print_header() {
    echo -e "\n${BLUE}=== $1 ===${NC}"
}

# Function to make curl request and display raw output
test_endpoint() {
    local method=$1
    local endpoint=$2
    local description=$3
    local data=$4
    
    echo -e "\n${YELLOW}$description${NC}"
    echo -e "${GREEN}$method $endpoint${NC}"
    
    if [ "$method" = "GET" ]; then
        curl -s "$BASE_URL$endpoint"
    elif [ "$method" = "POST" ] && [ -n "$data" ]; then
        curl -s -X POST -H "Content-Type: application/json" -d "$data" "$BASE_URL$endpoint"
    elif [ "$method" = "POST" ]; then
        curl -s -X POST -H "Content-Type: application/json" "$BASE_URL$endpoint"
    elif [ "$method" = "PUT" ] && [ -n "$data" ]; then
        curl -s -X PUT -H "Content-Type: application/json" -d "$data" "$BASE_URL$endpoint"
    elif [ "$method" = "PATCH" ] && [ -n "$data" ]; then
        curl -s -X PATCH -H "Content-Type: application/json" -d "$data" "$BASE_URL$endpoint"
    elif [ "$method" = "DELETE" ]; then
        curl -s -X DELETE "$BASE_URL$endpoint"
    fi
    echo -e "\n"
}

# Main page
print_header "HOME PAGE"
test_endpoint "GET" "/" "Home page"

# Health checks
print_header "HEALTH CHECKS"
test_endpoint "GET" "/health/" "Django health check"
test_endpoint "GET" "/api/v1/health/" "API health check"
test_endpoint "GET" "/api/v1/health/simple/" "Simple health check"
test_endpoint "GET" "/api/v1/health/performance/" "Performance metrics"

# API Documentation
print_header "API DOCUMENTATION"
test_endpoint "GET" "/api/schema/" "API schema"
test_endpoint "GET" "/api/docs/" "Swagger UI"
test_endpoint "GET" "/api/redoc/" "ReDoc documentation"

# Authentication endpoints
print_header "AUTHENTICATION"
test_endpoint "GET" "/api/v1/auth/status/" "Auth status"
test_endpoint "POST" "/api/v1/auth/login/" "Login" '{"username":"test","password":"test"}'
test_endpoint "POST" "/api/v1/auth/signup/" "Signup" '{"username":"test","email":"test@example.com","password":"test123"}'
test_endpoint "POST" "/api/v1/auth/logout/" "Logout"
test_endpoint "GET" "/api/v1/auth/user/" "Current user"
test_endpoint "POST" "/api/v1/auth/token/refresh/" "Refresh JWT token"
test_endpoint "POST" "/api/v1/auth/password/reset/" "Password reset" '{"email":"test@example.com"}'
test_endpoint "POST" "/api/v1/auth/password/change/" "Password change" '{"old_password":"old","new_password":"new"}'

# Social authentication
print_header "SOCIAL AUTHENTICATION"
test_endpoint "GET" "/api/v1/auth/social/providers/" "Social providers"
test_endpoint "GET" "/api/v1/auth/social/providers/available/" "Available providers"
test_endpoint "GET" "/api/v1/auth/social/connected/" "Connected providers"
test_endpoint "GET" "/api/v1/auth/social/status/" "Social auth status"
test_endpoint "POST" "/api/v1/auth/social/connect/google/" "Connect Google (example)"
test_endpoint "POST" "/api/v1/auth/social/disconnect/google/" "Disconnect Google (example)"

# Email verification
test_endpoint "POST" "/api/v1/auth/resend-verification/" "Resend email verification"
test_endpoint "GET" "/api/v1/auth/verify-email/sample-token/" "Verify email (sample token)"

# Parks API
print_header "PARKS API"
test_endpoint "GET" "/api/v1/parks/" "List parks"
test_endpoint "POST" "/api/v1/parks/" "Create park" '{"name":"Test Park","location":"Test Location"}'
test_endpoint "GET" "/api/v1/parks/hybrid/" "Hybrid park list"
test_endpoint "GET" "/api/v1/parks/hybrid/filter-metadata/" "Park filter metadata"
test_endpoint "GET" "/api/v1/parks/filter-options/" "Park filter options"
test_endpoint "GET" "/api/v1/parks/search/companies/" "Search companies"
test_endpoint "GET" "/api/v1/parks/search-suggestions/" "Park search suggestions"
test_endpoint "GET" "/api/v1/parks/1/" "Park detail (ID 1)"
test_endpoint "GET" "/api/v1/parks/sample-park/" "Park detail (slug)"
test_endpoint "GET" "/api/v1/parks/1/image-settings/" "Park image settings"

# Park photos
print_header "PARK PHOTOS"
test_endpoint "GET" "/api/v1/parks/1/photos/" "List park photos"
test_endpoint "POST" "/api/v1/parks/1/photos/" "Upload park photo"
test_endpoint "GET" "/api/v1/parks/1/photos/1/" "Park photo detail"

# Rides API
print_header "RIDES API"
test_endpoint "GET" "/api/v1/rides/" "List rides"
test_endpoint "POST" "/api/v1/rides/" "Create ride" '{"name":"Test Ride","park":1}'
test_endpoint "GET" "/api/v1/rides/hybrid/" "Hybrid ride filtering"
test_endpoint "GET" "/api/v1/rides/hybrid/filter-metadata/" "Ride filter metadata"
test_endpoint "GET" "/api/v1/rides/filter-options/" "Ride filter options"
test_endpoint "GET" "/api/v1/rides/search/companies/" "Search ride companies"
test_endpoint "GET" "/api/v1/rides/search/ride-models/" "Search ride models"
test_endpoint "GET" "/api/v1/rides/search-suggestions/" "Ride search suggestions"
test_endpoint "GET" "/api/v1/rides/1/" "Ride detail"
test_endpoint "GET" "/api/v1/rides/1/image-settings/" "Ride image settings"

# Ride photos
print_header "RIDE PHOTOS"
test_endpoint "GET" "/api/v1/rides/1/photos/" "List ride photos"
test_endpoint "POST" "/api/v1/rides/1/photos/" "Upload ride photo"
test_endpoint "GET" "/api/v1/rides/1/photos/1/" "Ride photo detail"

# Ride manufacturers and models
print_header "RIDE MANUFACTURERS & MODELS"
test_endpoint "GET" "/api/v1/rides/manufacturers/sample-manufacturer/" "Manufacturer ride models"
test_endpoint "POST" "/api/v1/rides/manufacturers/sample-manufacturer/" "Create ride model"
test_endpoint "GET" "/api/v1/rides/manufacturers/sample-manufacturer/search/" "Search ride models"
test_endpoint "GET" "/api/v1/rides/manufacturers/sample-manufacturer/filter-options/" "Model filter options"
test_endpoint "GET" "/api/v1/rides/manufacturers/sample-manufacturer/stats/" "Model stats"
test_endpoint "GET" "/api/v1/rides/manufacturers/sample-manufacturer/sample-model/" "Ride model detail"

# Ride model variants and specs
test_endpoint "GET" "/api/v1/rides/manufacturers/sample-manufacturer/sample-model/variants/" "Model variants"
test_endpoint "GET" "/api/v1/rides/manufacturers/sample-manufacturer/sample-model/variants/1/" "Variant detail"
test_endpoint "GET" "/api/v1/rides/manufacturers/sample-manufacturer/sample-model/technical-specs/" "Technical specs"
test_endpoint "GET" "/api/v1/rides/manufacturers/sample-manufacturer/sample-model/technical-specs/1/" "Tech spec detail"
test_endpoint "GET" "/api/v1/rides/manufacturers/sample-manufacturer/sample-model/photos/" "Model photos"
test_endpoint "GET" "/api/v1/rides/manufacturers/sample-manufacturer/sample-model/photos/1/" "Model photo detail"

# Rankings API
print_header "RANKINGS API"
test_endpoint "GET" "/api/v1/rankings/" "List rankings"
test_endpoint "POST" "/api/v1/rankings/calculate/" "Trigger ranking calculation"

# Trending API
print_header "TRENDING & NEW CONTENT"
test_endpoint "GET" "/api/v1/trending/" "Trending content"
test_endpoint "GET" "/api/v1/new-content/" "New content"
test_endpoint "POST" "/api/v1/trending/calculate/" "Trigger trending calculation"

# Statistics API
print_header "STATISTICS"
test_endpoint "GET" "/api/v1/stats/" "Site statistics"
test_endpoint "POST" "/api/v1/stats/recalculate/" "Recalculate statistics"

# Reviews API
print_header "REVIEWS"
test_endpoint "GET" "/api/v1/reviews/latest/" "Latest reviews"

# Account management
print_header "ACCOUNT MANAGEMENT"
test_endpoint "GET" "/api/v1/accounts/profile/" "User profile"
test_endpoint "PUT" "/api/v1/accounts/profile/update/" "Update profile"
test_endpoint "GET" "/api/v1/accounts/profile/account/" "Account info"
test_endpoint "PUT" "/api/v1/accounts/profile/account/" "Update account"

# User preferences
print_header "USER PREFERENCES"
test_endpoint "GET" "/api/v1/accounts/preferences/" "User preferences"
test_endpoint "PUT" "/api/v1/accounts/preferences/update/" "Update preferences"
test_endpoint "PUT" "/api/v1/accounts/preferences/theme/" "Update theme"

# Notification settings
test_endpoint "GET" "/api/v1/accounts/settings/notifications/" "Notification settings"
test_endpoint "PUT" "/api/v1/accounts/settings/notifications/update/" "Update notifications"

# Privacy settings
test_endpoint "GET" "/api/v1/accounts/settings/privacy/" "Privacy settings"
test_endpoint "PUT" "/api/v1/accounts/settings/privacy/update/" "Update privacy"

# Security settings
test_endpoint "GET" "/api/v1/accounts/settings/security/" "Security settings"
test_endpoint "PUT" "/api/v1/accounts/settings/security/update/" "Update security"

# User statistics and top lists
test_endpoint "GET" "/api/v1/accounts/statistics/" "User statistics"
test_endpoint "GET" "/api/v1/accounts/top-lists/" "User top lists"
test_endpoint "POST" "/api/v1/accounts/top-lists/create/" "Create top list"
test_endpoint "PUT" "/api/v1/accounts/top-lists/1/" "Update top list"
test_endpoint "DELETE" "/api/v1/accounts/top-lists/1/" "Delete top list"

# Notifications
print_header "NOTIFICATIONS"
test_endpoint "GET" "/api/v1/accounts/notifications/" "User notifications"
test_endpoint "POST" "/api/v1/accounts/notifications/mark-read/" "Mark notifications read"
test_endpoint "GET" "/api/v1/accounts/notification-preferences/" "Notification preferences"
test_endpoint "PUT" "/api/v1/accounts/notification-preferences/update/" "Update notification preferences"

# Avatar management
test_endpoint "POST" "/api/v1/accounts/profile/avatar/upload/" "Upload avatar"
test_endpoint "POST" "/api/v1/accounts/profile/avatar/save/" "Save avatar"
test_endpoint "DELETE" "/api/v1/accounts/profile/avatar/delete/" "Delete avatar"

# Account deletion
print_header "ACCOUNT DELETION"
test_endpoint "POST" "/api/v1/accounts/delete-account/request/" "Request account deletion"
test_endpoint "POST" "/api/v1/accounts/delete-account/verify/" "Verify account deletion"
test_endpoint "POST" "/api/v1/accounts/delete-account/cancel/" "Cancel account deletion"
test_endpoint "GET" "/api/v1/accounts/users/sample-id/deletion-check/" "Check deletion eligibility"
test_endpoint "DELETE" "/api/v1/accounts/users/sample-id/delete/" "Admin delete user"

# History API
print_header "HISTORY"
test_endpoint "GET" "/api/v1/history/timeline/" "Unified timeline"
test_endpoint "GET" "/api/v1/history/parks/sample-park/" "Park history list"
test_endpoint "GET" "/api/v1/history/parks/sample-park/detail/" "Park history detail"
test_endpoint "GET" "/api/v1/history/parks/sample-park/rides/sample-ride/" "Ride history list"
test_endpoint "GET" "/api/v1/history/parks/sample-park/rides/sample-ride/detail/" "Ride history detail"

# Email API
print_header "EMAIL"
test_endpoint "POST" "/api/v1/email/send/" "Send email" '{"to":"test@example.com","subject":"Test","message":"Test message"}'

# Core API
print_header "CORE SEARCH & ENTITIES"
test_endpoint "GET" "/api/v1/core/entities/search/" "Entity fuzzy search"
test_endpoint "GET" "/api/v1/core/entities/not-found/" "Entity not found"
test_endpoint "GET" "/api/v1/core/entities/suggestions/" "Quick entity suggestions"

# Maps API
print_header "MAPS"
test_endpoint "GET" "/api/v1/maps/locations/" "Map locations"
test_endpoint "GET" "/api/v1/maps/locations/park/1/" "Map location detail"
test_endpoint "GET" "/api/v1/maps/search/" "Map search"
test_endpoint "GET" "/api/v1/maps/bounds/" "Map bounds query"
test_endpoint "GET" "/api/v1/maps/stats/" "Map statistics"
test_endpoint "GET" "/api/v1/maps/cache/" "Map cache info"
test_endpoint "POST" "/api/v1/maps/cache/invalidate/" "Invalidate map cache"

# Moderation API
print_header "MODERATION"
test_endpoint "GET" "/api/v1/moderation/reports/" "List reports"
test_endpoint "POST" "/api/v1/moderation/reports/" "Create report"
test_endpoint "GET" "/api/v1/moderation/reports/1/" "Report detail"
test_endpoint "GET" "/api/v1/moderation/reports/stats/" "Report stats"
test_endpoint "POST" "/api/v1/moderation/reports/1/assign/" "Assign report"
test_endpoint "POST" "/api/v1/moderation/reports/1/resolve/" "Resolve report"

# Moderation queue
test_endpoint "GET" "/api/v1/moderation/queue/" "Moderation queue"
test_endpoint "GET" "/api/v1/moderation/queue/1/" "Queue item detail"
test_endpoint "GET" "/api/v1/moderation/queue/my_queue/" "My queue items"
test_endpoint "POST" "/api/v1/moderation/queue/1/assign/" "Assign queue item"
test_endpoint "POST" "/api/v1/moderation/queue/1/unassign/" "Unassign queue item"
test_endpoint "POST" "/api/v1/moderation/queue/1/complete/" "Complete queue item"

# Moderation actions
test_endpoint "GET" "/api/v1/moderation/actions/" "Moderation actions"
test_endpoint "GET" "/api/v1/moderation/actions/active/" "Active actions"
test_endpoint "GET" "/api/v1/moderation/actions/expired/" "Expired actions"
test_endpoint "POST" "/api/v1/moderation/actions/1/deactivate/" "Deactivate action"

# Bulk operations
test_endpoint "GET" "/api/v1/moderation/bulk-operations/" "Bulk operations"
test_endpoint "GET" "/api/v1/moderation/bulk-operations/running/" "Running operations"
test_endpoint "GET" "/api/v1/moderation/bulk-operations/1/" "Bulk operation detail"
test_endpoint "GET" "/api/v1/moderation/bulk-operations/1/logs/" "Operation logs"
test_endpoint "POST" "/api/v1/moderation/bulk-operations/1/cancel/" "Cancel operation"
test_endpoint "POST" "/api/v1/moderation/bulk-operations/1/retry/" "Retry operation"

# User moderation
test_endpoint "GET" "/api/v1/moderation/users/1/" "User moderation profile"
test_endpoint "POST" "/api/v1/moderation/users/1/moderate/" "Moderate user"
test_endpoint "GET" "/api/v1/moderation/users/search/" "Search users for moderation"
test_endpoint "GET" "/api/v1/moderation/users/stats/" "User moderation stats"

# Cloudflare Images Toolkit
print_header "CLOUDFLARE IMAGES"
test_endpoint "GET" "/api/v1/cloudflare-images/" "Cloudflare Images endpoints (varies by toolkit)"

# Environment and settings
print_header "ENVIRONMENT & SETTINGS"
test_endpoint "GET" "/env-settings/" "Environment and settings"

# Static pages
print_header "STATIC PAGES"
test_endpoint "GET" "/terms/" "Terms of service"
test_endpoint "GET" "/privacy/" "Privacy policy"

echo -e "\n${GREEN}=== All endpoint tests completed ===${NC}"