#!/bin/bash

# Week 1 Day 4: Create Components for PROD Project
# This script runs AFTER PROD project is created

# Atlassian Cloud credentials (hardcoded for reliable sourcing in SSH contexts)
ATLASSIAN_SITE_URL="https://ai-impress.atlassian.net"
JIRA_AUTH="di5zYW1vaWxlbmtvQGFpLWltcHJlc3MuY29tOkFUQVRUM3hGZkdGMEFCbEppNE1PUkoxTWx5YjZaaFItSTk4NFRZQ3JVaG9HSDN5SGYwYmpoWktrSl9wazI3czZtcmItR1ZvVnRyMGJYbVhXdFlEeVF0MFFMMFV0NkJOcG1mcnoxQVRKbklicUFzV1Z1V2VLSHhxeUtKOGdaVkFwc2k4T0JjLWpDMkJWb0c5VFVFQkRQRE1XbUdfMEpHM3pGVTZidjhqVG1HZWN3ZTJ4bFp6VGlKbz1FMDA5MzE0MA=="

echo "=== Week 1 Day 4: Creating PROD Components ==="
echo ""
echo "Note: This script runs AFTER PROD project is created"
echo "Component leads should be assigned based on team:"
echo "  - Vadym Samoilenko: Frontend, Backend, Integrations"
echo "  - Stanislava Klochok: CMS, SEO, Design Assets"
echo ""

# Function to create component
create_component() {
    local name=$1
    local description=$2
    local lead_id=$3

    echo "Creating component: $name..."

    # Create component using REST API v3 - correct endpoint is /rest/api/3/component
    curl -s -X POST \
      "${ATLASSIAN_SITE_URL}/rest/api/3/component" \
      -H "Authorization: Basic ${JIRA_AUTH}" \
      -H "Content-Type: application/json" \
      -d "{\"name\": \"$name\", \"description\": \"$description\", \"project\": \"PROD\"}" > /tmp/component_response.json

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

echo "IMPORTANT: Replace <ACCOUNT_ID> placeholders with actual Jira account IDs"
echo "Get account IDs from: Settings → People → Directory"
echo ""

# PROD Components (6 total)
# Using placeholder account IDs - replace with actual Jira account IDs
VADYM_ID="<VADYM_ACCOUNT_ID>"
STANISLAVA_ID="<STANISLAVA_ACCOUNT_ID>"

create_component \
    "Website-Frontend" \
    "Next.js/React UI, landing pages, responsive design" \
    "$VADYM_ID"

create_component \
    "Website-Backend" \
    "API endpoints, forms processing, data models" \
    "$VADYM_ID"

create_component \
    "Website-CMS" \
    "Content management, blog, page builder" \
    "$STANISLAVA_ID"

create_component \
    "Integrations" \
    "n8n workflows, Calendly, form submissions, email" \
    "$VADYM_ID"

create_component \
    "SEO" \
    "Meta tags, sitemap, analytics, structured data" \
    "$STANISLAVA_ID"

create_component \
    "Design-Assets" \
    "Images, icons, brand elements, design system" \
    "$STANISLAVA_ID"

echo "✓ Components creation complete"
echo ""
echo "Next: Run 06-create-filters.sh"
