#!/bin/bash

source /tmp/atlassian-setup/credentials.env

echo "=== Week 1 Day 2: Creating 4 Jira Projects ==="
echo ""

# Save all responses to file for documentation
RESPONSES="/tmp/atlassian-setup/project-creation-responses.log"
> "$RESPONSES"

# Function to create project
create_project() {
    local key=$1
    local name=$2
    local template=$3
    local description=$4
    
    echo "Creating $key - $name ($template)..."
    
    local response=$(curl -s -X POST \
      "${ATLASSIAN_SITE_URL}/rest/api/3/projects" \
      -H "Authorization: Basic ${JIRA_AUTH}" \
      -H "Content-Type: application/json" \
      -d "{
        \"key\": \"$key\",
        \"name\": \"$name\",
        \"description\": \"$description\",
        \"projectTypeKey\": \"software\",
        \"projectTemplateKey\": \"com.atlassian.jira-core-project-templates:jira-${template}-project\"
      }")
    
    echo "$response" >> "$RESPONSES"
    
    # Check if successful (look for key in response)
    if echo "$response" | grep -q "\"key\":\"$key\""; then
        echo "✓ $key created successfully"
    else
        echo "⚠ $key response: $(echo $response | cut -c1-100)..."
    fi
    echo ""
}

# Create all 4 projects
create_project "PROD" "Product & Engineering" "scrum" "Website development and client project delivery"
create_project "MARK" "Marketing & Sales" "kanban" "Sales pipeline, leads, clients, and marketing campaigns"
create_project "SUPP" "Customer Support" "service-desk" "Client tickets, technical support, and escalations"
create_project "OPS" "Operations" "business" "Finance, contracts, HR, and compliance"

echo "See $RESPONSES for full API responses"
