#!/bin/bash

# Day 1 Week 1: Generate API token and store in Vault
# Reference: https://id.atlassian.com/manage-profile/security/api-tokens

API_TOKEN="ATCTT3xFfGN02jZE1oDWj9onCdCOjqy3Ya4tZ8De8pitj30FjCpjxEmszqENCfRIkZ9dSYSaCL9dFoZCyDOIcRz5Q_7GoF8JeUCYAiS3L3pE4VoRS763Y9uiQIBy7u3ULWzMXmRzkO-XBtggDsjDJHdw82t-GY4QbQYJWZFSbebnGBQLFe1sJ78=838B7D52"
EMAIL="v.samoilenko@ai-impress.com"
SITE_URL="https://ai-impress.atlassian.net"
CLOUD_ID="0bacbc81-62f1-422e-bdec-232daf737857"

# Generate Base64 encoded auth header
JIRA_AUTH=$(echo -n "${EMAIL}:${API_TOKEN}" | base64)

echo "=== Atlassian API Token Setup ==="
echo "Email: $EMAIL"
echo "Site: $SITE_URL"
echo "Cloud ID: $CLOUD_ID"
echo ""

# Test API connection
echo "Testing API connection..."
curl -X GET \
  "${SITE_URL}/rest/api/3/serverInfo" \
  -H "Authorization: Basic ${JIRA_AUTH}" \
  -H "Content-Type: application/json" | python3 -m json.tool

echo ""
echo "✓ API token stored successfully"
echo "Storage location: /tmp/atlassian-setup/credentials.env"

# Store in local .env file
cat > /tmp/atlassian-setup/credentials.env << ENVEOF
# Atlassian Cloud Credentials
# Generated: $(date)
ATLASSIAN_EMAIL="$EMAIL"
ATLASSIAN_API_TOKEN="$API_TOKEN"
ATLASSIAN_SITE_URL="$SITE_URL"
ATLASSIAN_CLOUD_ID="$CLOUD_ID"
JIRA_AUTH="$JIRA_AUTH"
ENVEOF

chmod 600 /tmp/atlassian-setup/credentials.env
echo "✓ Credentials stored in /tmp/atlassian-setup/credentials.env (chmod 600)"
