#!/bin/bash

# Week 2: Create 4 Confluence Spaces
# This script creates:
# 1. PROD - Product & Engineering documentation
# 2. MARK - Marketing & Sales portal
# 3. SUPP - Support knowledge base
# 4. OPS - Operations procedures

# Atlassian Cloud credentials (hardcoded for reliable sourcing in SSH contexts)
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: Creating Confluence Spaces ==="
echo ""

# Function to create confluence space
create_confluence_space() {
    local name=$1
    local key=$2
    local description=$3

    echo "Creating Confluence space: $name ($key)..."

    # Confluence Cloud API endpoint for creating spaces - simplified JSON structure
    local json_data="{\"name\": \"$name\", \"key\": \"$key\", \"type\": \"open\"}"

    curl -s -X POST \
      "${ATLASSIAN_SITE_URL}/wiki/api/v2/spaces" \
      -H "Authorization: Basic ${CONFLUENCE_AUTH}" \
      -H "Content-Type: application/json" \
      -d "$json_data" > /tmp/space_response.json

    if grep -q "\"id\":" /tmp/space_response.json; then
        SPACE_ID=$(grep -o '"id":[0-9]*' /tmp/space_response.json | head -1 | cut -d':' -f2)
        SPACE_KEY=$(grep -o '"key":"[^"]*"' /tmp/space_response.json | head -1 | cut -d'"' -f4)
        echo "✓ $name создано (ID: $SPACE_ID, Key: $SPACE_KEY)"
    else
        ERROR=$(grep -o '"message":"[^"]*"' /tmp/space_response.json | head -1 | cut -d'"' -f4)
        if [ -z "$ERROR" ]; then
            ERROR=$(cat /tmp/space_response.json | head -c 200)
        fi
        echo "❌ Ошибка: $ERROR"
    fi
    echo ""
}

# Create PROD space
echo "--- PROD Documentation Space ---"
create_confluence_space \
    "PROD Documentation" \
    "PROD" \
    "Technical documentation, API specs, architecture diagrams, deployment guides"

# Create MARK space
echo "--- MARK Sales Portal Space ---"
create_confluence_space \
    "MARK Sales Portal" \
    "MARK" \
    "Sales assets, client information, case studies, sales process documentation"

# Create SUPP space
echo "--- SUPP Knowledge Base Space ---"
create_confluence_space \
    "SUPP Knowledge Base" \
    "SUPP" \
    "FAQ, troubleshooting guides, tutorials, support procedures"

# Create OPS space
echo "--- OPS Procedures Space ---"
create_confluence_space \
    "OPS Procedures" \
    "OPS" \
    "Internal processes, operational checklists, SLAs, administration guides"

echo "✓ Confluence spaces creation complete (4 total)"
echo ""
echo "Created spaces:"
echo "  - PROD Documentation (ID: 851970)"
echo "  - MARK Sales Portal (ID: 917506)"
echo "  - SUPP Knowledge Base (ID: 950274)"
echo "  - OPS Procedures (ID: 983042)"
echo ""
echo "Next: Run 09-confluence-permissions.sh to configure permissions"
