#!/bin/bash

# Week 2: Configure Confluence Space Permissions
# Sets up access control for each space

ATLASSIAN_SITE_URL="https://ai-impress.atlassian.net"
ATLASSIAN_EMAIL="v.samoilenko@ai-impress.com"
ATLASSIAN_API_TOKEN="ATATT3xFfGF0ABlJi4MORJ1Mlyb6ZhR-I984TYCrUhoGH3yHf0bjhZKkJ_pk27s6mrb-GVoVtr0bXmXWtYDyQt0QL0Ut6BNpmfrz1ATJnIbqAsWVuWeKHxqyKJ8gZVApsi8OBc-jC2BVoG9TUEBDPDMWmG_0JG3zFU6bv8jTmGecwe2xlZzTiJo=E0093140"
CONFLUENCE_AUTH=$(echo -n "${ATLASSIAN_EMAIL}:${ATLASSIAN_API_TOKEN}" | base64)

echo "=== Week 2: Configuring Confluence Space Permissions ==="
echo ""
echo "Note: Permission configuration typically requires manual UI access"
echo "This script provides instructions for setting up permissions"
echo ""

# Function to list space details
get_space_info() {
    local space_key=$1

    curl -s -X GET \
      "${ATLASSIAN_SITE_URL}/wiki/api/v2/spaces/$space_key" \
      -H "Authorization: Basic ${CONFLUENCE_AUTH}" \
      -H "Content-Type: application/json"
}

echo "--- PROD Documentation (PROD) ---"
echo "Access level: RESTRICTED"
echo "Who should access: Product & Engineering team"
echo "Permissions to configure:"
echo "  - PROD team: Can view and edit"
echo "  - Stanislava: Can view and edit"
echo "  - Others: Can view"
echo ""

echo "--- MARK Sales Portal (MARK) ---"
echo "Access level: RESTRICTED"
echo "Who should access: Marketing & Sales team"
echo "Permissions to configure:"
echo "  - MARK team: Can view and edit"
echo "  - Sales leads: Can view and edit"
echo "  - Others: Can view"
echo ""

echo "--- SUPP Knowledge Base (SUPP) ---"
echo "Access level: PUBLIC"
echo "Who should access: All support team + customers"
echo "Permissions to configure:"
echo "  - Support team: Can view and edit"
echo "  - Customers: Can view"
echo ""

echo "--- OPS Procedures (OPS) ---"
echo "Access level: RESTRICTED"
echo "Who should access: Operations team only"
echo "Permissions to configure:"
echo "  - OPS team: Can view and edit"
echo "  - Admin: Can view and edit"
echo ""

echo "=== To Configure Permissions Manually ==="
echo ""
echo "1. Go to https://ai-impress.atlassian.net/wiki/spaces"
echo "2. For each space (PROD, MARK, SUPP, OPS):"
echo "   - Click on space"
echo "   - Go to 'Space settings' → 'Permissions'"
echo "   - Add team members and set appropriate permission levels"
echo ""

# Verify spaces exist
echo "=== Verifying Created Spaces ==="
echo ""

for space_key in PROD MARK SUPP OPS; do
    INFO=$(get_space_info "$space_key")
    if echo "$INFO" | grep -q "\"id\""; then
        NAME=$(echo "$INFO" | python3 -c "import sys, json; data=json.load(sys.stdin); print(data.get('name', 'Unknown'))")
        ID=$(echo "$INFO" | python3 -c "import sys, json; data=json.load(sys.stdin); print(data.get('id', 'Unknown'))")
        echo "✓ $space_key - $NAME (ID: $ID)"
    else
        echo "❌ $space_key - Not found"
    fi
done

echo ""
echo "=== Space Configuration Summary ==="
echo ""
echo "✓ 4 Confluence spaces created successfully"
echo "  - PROD Documentation"
echo "  - MARK Sales Portal"
echo "  - SUPP Knowledge Base"
echo "  - OPS Procedures"
echo ""
echo "Next: Configure permissions and add team members via UI"
