#!/bin/bash

# Categories generator for LibrePortal
# Creates apps-config-categories.json file

webuiCreateAppsConfigCategories() {
    local output_dir="$1"
    
    if [[ -z "$output_dir" ]]; then
        isError " Output directory not specified"
    fi
    
    echo "Creating apps-config-categories.json..."
    
    # Create output directory if it doesn't exist
    runFileOp mkdir -p "$output_dir"
    
    # Create temp file first, then atomic move
    local temp_file="$(mktemp)"
    local final_file="${output_dir}/apps-config-categories.json"
    
    # Generate apps-config-categories.json
    cat > "$temp_file" << 'EOF'
{
  "categories": {
    "general": {
      "name": "General",
      "icon": "🎯",
      "description": "Basic application settings",
      "order": 1
    },
    "network": {
      "name": "Network",
      "icon": "🌐",
      "description": "Network and connectivity settings",
      "order": 2
    },
    "ports": {
      "name": "Ports",
      "icon": "🔌",
      "description": "Configure application ports and services",
      "order": 3
    },
    "access": {
      "name": "Features",
      "icon": "🔐",
      "description": "Authentication, access control and integration features",
      "order": 4
    },
    "features": {
      "name": "Integrations",
      "icon": "🔗",
      "description": "Application deployment and runtime settings",
      "order": 5
    },
    "notifications": {
      "name": "Notifications",
      "icon": "🔔",
      "description": "Channels this app uses to send notifications",
      "order": 6
    },
    "advanced": {
      "name": "Advanced",
      "icon": "🔬",
      "description": "Expert settings that may break your application if configured incorrectly",
      "order": 10
    },
    "system": {
      "name": "System",
      "icon": "🔧",
      "description": "System integration and deployment options",
      "order": 7
    }
  }
}
EOF

    # Atomic move to final location
    if [ $? -eq 0 ]; then
        runFileWrite "$final_file" < "$temp_file"; rm -f "$temp_file"
    else
        rm -f "$temp_file" 2>/dev/null
    fi
}
