#!/bin/bash
#
# Phase 2: Azure Infrastructure Setup
# Terraform infrastructure deployment
#

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${SCRIPT_DIR}/config.sh"

log_info "=========================================="
log_info "Phase 2: Azure Infrastructure Setup"
log_info "=========================================="

# 2.1 Azure Subscription Preparation
log_step "2.1 Preparing Azure subscription..."

cd "${PROJECT_ROOT}"

# Run Azure setup scripts
if [ -f "${INFRA_DIR}/scripts/azure-setup.sh" ]; then
  log_info "Running Azure setup script..."
  bash "${INFRA_DIR}/scripts/azure-setup.sh" || error_exit "Azure setup script failed"
else
  log_warning "Azure setup script not found, skipping..."
fi

# Register resource providers
if [ -f "${INFRA_DIR}/scripts/azure-register-providers.sh" ]; then
  log_info "Registering Azure resource providers..."
  bash "${INFRA_DIR}/scripts/azure-register-providers.sh" || error_exit "Provider registration failed"
else
  log_warning "Provider registration script not found, skipping..."
fi

# 2.2 Terraform Infrastructure Deployment
log_step "2.2 Deploying Terraform infrastructure..."

cd "${TERRAFORM_DIR}"

# Initialize Terraform
log_info "Initializing Terraform..."
terraform init || error_exit "Terraform initialization failed"

# Create initial state storage if needed
if [ "${CREATE_STATE_STORAGE:-false}" = "true" ]; then
  log_info "Creating Terraform state storage..."
  terraform plan -target=azurerm_resource_group.terraform_state \
                 -target=azurerm_storage_account.terraform_state \
                 -target=azurerm_storage_container.terraform_state \
    || log_warning "State storage resources may already exist"
  
  terraform apply -target=azurerm_resource_group.terraform_state \
                  -target=azurerm_storage_account.terraform_state \
                  -target=azurerm_storage_container.terraform_state \
    || log_warning "State storage may already exist"
fi

# Plan infrastructure
log_info "Planning infrastructure changes..."
terraform plan -out=tfplan || error_exit "Terraform plan failed"

# Review plan (optional)
if [ "${AUTO_APPLY:-false}" != "true" ]; then
  log_warning "Terraform plan created. Review tfplan before applying."
  log_info "To apply: terraform apply tfplan"
  log_info "To auto-apply: set AUTO_APPLY=true"
else
  log_info "Applying Terraform plan..."
  terraform apply tfplan || error_exit "Terraform apply failed"
  log_success "Infrastructure deployed"
fi

# Get outputs
log_info "Retrieving Terraform outputs..."
terraform output -json > "${STATE_DIR}/terraform-outputs.json" || log_warning "Failed to save outputs"

# 2.3 Kubernetes Configuration
log_step "2.3 Configuring Kubernetes..."

# Get AKS credentials
log_info "Getting AKS credentials..."
az aks get-credentials --resource-group "${AKS_RESOURCE_GROUP}" \
                       --name "${AKS_NAME}" \
                       --overwrite-existing \
  || log_warning "AKS cluster may not exist yet"

# Verify cluster access
if kubectl cluster-info &> /dev/null; then
  log_success "Kubernetes cluster accessible"
  kubectl get nodes || log_warning "No nodes found"
else
  log_warning "Kubernetes cluster not accessible yet"
fi

# Save state
save_state "phase2" "complete"

log_success "=========================================="
log_success "Phase 2: Azure Infrastructure - COMPLETE"
log_success "=========================================="

