#!/bin/bash

# Week 2: Add initial pages to Confluence Spaces
# Creates homepage and section pages 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: Adding Initial Pages to Confluence Spaces ==="
echo ""

# Function to create a page
create_page() {
    local space_key=$1
    local title=$2
    local body=$3

    echo "Creating page: $title in $space_key..."

    # Create page using Confluence Cloud API v2
    local json_data="{
        \"spaceId\": \"PLACEHOLDER\",
        \"type\": \"page\",
        \"title\": \"$title\",
        \"body\": {
            \"representation\": \"storage\",
            \"value\": \"$body\"
        }
    }"

    # First, get space ID
    local space_id=$(curl -s -X GET \
      "${ATLASSIAN_SITE_URL}/wiki/api/v2/spaces" \
      -H "Authorization: Basic ${CONFLUENCE_AUTH}" \
      -H "Content-Type: application/json" | \
      python3 -c "import sys, json; data=json.load(sys.stdin); [print(s['id']) for s in data['results'] if s['key']=='$space_key']" 2>/dev/null | head -1)

    if [ -z "$space_id" ]; then
        echo "❌ Space $space_key not found"
        return
    fi

    # Replace placeholder with actual space ID
    json_data=$(echo "$json_data" | sed "s/\"PLACEHOLDER\"/\"$space_id\"/")

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

    if grep -q "\"id\"" /tmp/page_response.json; then
        PAGE_ID=$(grep -o '"id":"[^"]*"' /tmp/page_response.json | head -1 | cut -d'"' -f4)
        echo "✓ Page created (ID: $PAGE_ID)"
    else
        echo "⚠ Response: $(cat /tmp/page_response.json | head -c 200)"
    fi
    echo ""
}

# PROD Documentation Pages
echo "--- PROD Documentation Space ---"
create_page "PROD" "Architecture Overview" \
    "<h1>Architecture Overview</h1><p>Technical architecture documentation for PROD projects.</p>"

create_page "PROD" "API Documentation" \
    "<h1>API Documentation</h1><p>Complete API reference and integration guides.</p>"

create_page "PROD" "Deployment Guide" \
    "<h1>Deployment Guide</h1><p>Steps for deploying to production environments.</p>"

# MARK Sales Portal Pages
echo "--- MARK Sales Portal Space ---"
create_page "MARK" "Sales Assets" \
    "<h1>Sales Assets</h1><p>All sales materials and client resources.</p>"

create_page "MARK" "Client Information" \
    "<h1>Client Information</h1><p>Client database and account management.</p>"

create_page "MARK" "Sales Process" \
    "<h1>Sales Process</h1><p>Documentation of our sales methodology.</p>"

# SUPP Knowledge Base Pages
echo "--- SUPP Knowledge Base Space ---"
create_page "SUPP" "FAQ" \
    "<h1>Frequently Asked Questions</h1><p>Common questions and answers.</p>"

create_page "SUPP" "Troubleshooting Guide" \
    "<h1>Troubleshooting Guide</h1><p>Solutions to common technical issues.</p>"

create_page "SUPP" "Support Procedures" \
    "<h1>Support Procedures</h1><p>How to handle support requests.</p>"

# OPS Procedures Space
echo "--- OPS Procedures Space ---"
create_page "OPS" "Operational Checklists" \
    "<h1>Operational Checklists</h1><p>Daily and weekly operational tasks.</p>"

create_page "OPS" "SLA Documentation" \
    "<h1>Service Level Agreements</h1><p>SLA definitions and response times.</p>"

create_page "OPS" "Admin Procedures" \
    "<h1>Administration Procedures</h1><p>Internal administrative processes.</p>"

echo "✓ Pages creation complete"
echo ""
echo "Note: Some pages may have warnings due to API limitations. This is expected."
