#!/bin/bash

# Master Atlassian Setup Script
# Week 1-5 Implementation

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CONFIG_FILE="/opt/00-infrastructure/atlassian/atlassian-config.json"
SCRIPTS_DIR="/opt/00-infrastructure/atlassian/scripts"
CREDS_FILE="/opt/00-infrastructure/atlassian/.env.atlassian"

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

# Functions
print_header() {
    echo -e "${BLUE}=== $1 ===${NC}"
}

print_success() {
    echo -e "${GREEN}✓ $1${NC}"
}

print_warning() {
    echo -e "${YELLOW}⚠ $1${NC}"
}

print_error() {
    echo -e "${RED}✗ $1${NC}"
}

# Load credentials
load_credentials() {
    if [ -f "$CREDS_FILE" ]; then
        source "$CREDS_FILE"
        print_success "Credentials loaded"
    else
        print_error "Credentials file not found: $CREDS_FILE"
        exit 1
    fi
}

# Check API connectivity
check_api() {
    print_header "Checking API Connectivity"
    
    local response=$(curl -s -w "\n%{http_code}" \
        "${ATLASSIAN_SITE_URL}/rest/api/3/serverInfo" \
        -H "Authorization: Basic ${JIRA_AUTH}")
    
    local http_code=$(echo "$response" | tail -n 1)
    
    if [ "$http_code" = "200" ]; then
        print_success "API connection working (HTTP $http_code)"
    else
        print_error "API connection failed (HTTP $http_code)"
        exit 1
    fi
}

# Phase 1: Foundation
phase_1() {
    print_header "Phase 1: Foundation & Authentication"
    
    load_credentials
    check_api
    
    print_success "Phase 1 complete"
    echo ""
    echo "NEXT STEPS:"
    echo "1. Go to: ${ATLASSIAN_SITE_URL}"
    echo "2. Create 4 projects via UI:"
    echo "   - PROD (Scrum)"
    echo "   - MARK (Kanban)"
    echo "   - SUPP (Service Desk)"
    echo "   - OPS (Business)"
    echo "3. After projects created, run: phase_2"
}

# Phase 2: Configuration
phase_2() {
    print_header "Phase 2: Custom Fields, Components, Filters"
    
    load_credentials
    
    # Verify projects exist
    print_header "Verifying projects..."
    bash "${SCRIPTS_DIR}/04-create-custom-fields.sh"
    bash "${SCRIPTS_DIR}/05-create-components.sh"
    bash "${SCRIPTS_DIR}/06-create-filters.sh"
    
    print_success "Phase 2 complete"
}

# Phase 3: Advanced
phase_3() {
    print_header "Phase 3: Confluence, Dashboards, Automation"
    
    print_warning "Phase 3 requires manual UI configuration for:"
    echo "  - 4 Confluence spaces"
    echo "  - 4 Dashboards"
    echo "  - 15 Automation rules"
}

# Main menu
main() {
    clear
    print_header "Atlassian Cloud Setup - AImpress Ltd"
    echo ""
    echo "1. Phase 1: Authentication & Verification"
    echo "2. Phase 2: Custom Fields, Components, Filters"
    echo "3. Phase 3: Confluence, Dashboards, Automation"
    echo "4. Check API Status"
    echo "5. View Configuration"
    echo "6. Exit"
    echo ""
    read -p "Select option: " choice
    
    case $choice in
        1) phase_1 ;;
        2) phase_2 ;;
        3) phase_3 ;;
        4) load_credentials && check_api ;;
        5) cat "$CONFIG_FILE" | jq . ;;
        6) exit 0 ;;
        *) print_error "Invalid option" && main ;;
    esac
}

# Run
if [ $# -eq 0 ]; then
    main
else
    case $1 in
        phase1) phase_1 ;;
        phase2) phase_2 ;;
        phase3) phase_3 ;;
        check) load_credentials && check_api ;;
        config) cat "$CONFIG_FILE" | jq . ;;
        *) 
            echo "Usage: $0 [phase1|phase2|phase3|check|config]"
            exit 1
            ;;
    esac
fi
