#!/bin/bash

# Script to remove X-Client-Key and GetClientID from all modules
# Replace with auth token approach

echo "Starting update of all modules to remove X-Client-Key..."

# List of modules to update
modules=(
    "articles"
    "users" 
    "article_categories"
    "magazines"
    "schedules"
    "subscription"
    "user_levels"
    "feedbacks"
    "advertisement"
    "article_comments"
    "article_approvals"
    "article_approval_flows"
    "client_approval_settings"
    "bookmarks"
    "approval_workflows"
    "article_files"
    "custom_static_pages"
    "article_approval_step_logs"
    "approval_workflow_steps"
    "activity_logs"
)

for module in "${modules[@]}"; do
    echo "Updating module: $module"
    
    controller_file="app/module/$module/controller/${module}.controller.go"
    
    if [ -f "$controller_file" ]; then
        echo "  - Updating controller: $controller_file"
        
        # Remove X-Client-Key from Swagger docs
        sed -i 's|// @Param        X-Client-Key header string true "Insert the X-Client-Key"||g' "$controller_file"
        sed -i 's|// @Param        X-Client-Key header string false "Insert the X-Client-Key"||g' "$controller_file"
        
        # Remove GetClientID calls
        sed -i 's|clientId := middleware\.GetClientID(c)||g' "$controller_file"
        sed -i 's|// Get ClientId from context||g' "$controller_file"
        
        # Remove middleware import if no longer needed
        if ! grep -q "middleware\." "$controller_file"; then
            sed -i '/"netidhub-saas-be\/app\/middleware"/d' "$controller_file"
        fi
        
        echo "  - Updated: $controller_file"
    else
        echo "  - Controller not found: $controller_file"
    fi
done

echo "Update completed!"
echo ""
echo "Manual steps required:"
echo "1. Update service interfaces to use authToken instead of clientId"
echo "2. Update service implementations to extract clientId from authToken"
echo "3. Update repository calls to use extracted clientId"
echo "4. Test all endpoints"
