#!/bin/bash

# Week 1 Day 2: Create Projects via API
# Using correct REST API v3 endpoint: POST /rest/api/3/project
# Reference: https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-projects/

source /tmp/atlassian-setup/credentials.env

echo "=== Creating Jira Projects via REST API v3 ==="
echo ""

# Function to create project
create_project() {
    local key=$1
    local name=$2
    local project_type=$3
    local template_key=$4
    local description=$5
    local lead_id=$6
    
    echo "Creating project: $key - $name ($project_type)"
    
    # Correct payload based on Atlassian example
    local json_payload=$(cat <<JSONEOF
{
  "key": "$key",
  "name": "$name",
  "projectTypeKey": "$project_type",
  "projectTemplateKey": "$template_key",
  "description": "$description"
}
JSONEOF
)
    
    echo "Payload:"
    echo "$json_payload" | jq .
    echo ""
    
    local response=$(curl -s -w "\n%{http_code}" -X POST \
        "${ATLASSIAN_SITE_URL}/rest/api/3/project" \
        -H "Authorization: Basic ${JIRA_AUTH}" \
        -H "Content-Type: application/json" \
        -d "$json_payload")
    
    local http_code=$(echo "$response" | tail -n 1)
    local body=$(echo "$response" | sed '$d')
    
    echo "Response (HTTP $http_code):"
    if [ "$http_code" = "201" ]; then
        echo "$body" | jq .
        echo "✓ $key created successfully"
    elif [ "$http_code" = "400" ]; then
        echo "⚠ Bad Request (HTTP 400)"
        echo "$body" | jq . 2>/dev/null || echo "$body"
    elif [ "$http_code" = "403" ]; then
        echo "⚠ Forbidden (HTTP 403) - Insufficient permissions"
        echo "$body" | jq . 2>/dev/null || echo "$body"
    else
        echo "Response: $body"
    fi
    echo ""
}

echo "Template Keys Reference:"
echo "  scrum: com.atlassian.jira-core-project-templates:jira-scrum-project"
echo "  kanban: com.atlassian.jira-core-project-templates:jira-kanban-project"
echo "  business: com.atlassian.jira-core-project-templates:jira-core-simplified-process-control"
echo "  service-desk: com.atlassian.servicedesk.project-templates:it-help-desk"
echo ""

# Create projects
# Format: key, name, projectTypeKey, projectTemplateKey, description
create_project \
    "PROD" \
    "Product & Engineering" \
    "software" \
    "com.atlassian.jira-core-project-templates:jira-scrum-project" \
    "Website development and client project delivery"

create_project \
    "MARK" \
    "Marketing & Sales" \
    "software" \
    "com.atlassian.jira-core-project-templates:jira-kanban-project" \
    "Sales pipeline, leads, clients, and marketing campaigns"

create_project \
    "SUPP" \
    "Customer Support" \
    "service_management" \
    "com.atlassian.servicedesk.project-templates:it-help-desk" \
    "Client tickets, technical support, and escalations"

create_project \
    "OPS" \
    "Operations" \
    "business" \
    "com.atlassian.jira-core-project-templates:jira-core-simplified-process-control" \
    "Finance, contracts, HR, and compliance"

echo "=== Project Creation Complete ==="
echo ""
echo "Next steps:"
echo "1. Verify projects created: /opt/00-infrastructure/atlassian/atlassian-setup.sh check"
echo "2. Run phase 2 to create custom fields: ./scripts/04-create-custom-fields.sh"
