#!/bin/bash
# Complete Azure CDN Setup for Credential Seals
# Orchestrates quota check, infrastructure setup, and file upload

set -euo pipefail

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

log_info() { echo -e "${BLUE}[SETUP]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_warning() { echo -e "${YELLOW}[!]${NC} $1"; }
log_error() { echo -e "${RED}[✗]${NC} $1"; }

cd "$(dirname "$0")/../.."

echo ""
log_info "=== Complete Azure CDN Setup for Credential Seals ==="
echo ""

# Step 1: Check quotas
log_info "Step 1: Checking Azure quotas..."
if ./infra/scripts/azure-check-cdn-quotas.sh; then
    log_success "Quota check passed"
else
    log_warning "Quota check found issues (review azure-cdn-quota-report.txt)"
    log_info "Continuing with setup (review quotas manually if needed)..."
fi
echo ""

# Step 2: Set up Azure infrastructure
log_info "Step 2: Setting up Azure infrastructure..."
if ./infra/scripts/azure-cdn-setup.sh; then
    log_success "Azure infrastructure setup complete"
else
    log_error "Azure infrastructure setup failed"
    exit 1
fi
echo ""

# Step 3: Load configuration
log_info "Step 3: Loading configuration..."
if [ -f "azure-cdn-config.env" ]; then
    source azure-cdn-config.env
    log_success "Configuration loaded"
else
    log_error "Configuration file not found: azure-cdn-config.env"
    exit 1
fi
echo ""

# Step 4: Prepare PNG files (if not already done)
log_info "Step 4: Checking PNG files..."
PNG_COUNT=$(find assets/credential-images/png -name "*.png" -type f 2>/dev/null | wc -l)
if [ "${PNG_COUNT}" -eq 0 ]; then
    log_info "PNG files not found, generating..."
    ./scripts/deploy/prepare-all-credential-seals.sh
else
    log_success "PNG files found: ${PNG_COUNT}"
fi
echo ""

# Step 5: Upload files to Azure
log_info "Step 5: Uploading files to Azure Blob Storage..."
if ./scripts/deploy/upload-seals-to-azure.sh; then
    log_success "Files uploaded successfully"
else
    log_error "File upload failed"
    exit 1
fi
echo ""

# Step 6: Update manifest URLs
log_info "Step 6: Updating manifest templates with CDN URLs..."
if [ -n "${CDN_BASE_URL_CDN:-}" ] && [ "${CDN_BASE_URL_CDN}" != "https://.azureedge.net/images/" ]; then
    CDN_BASE_URL="${CDN_BASE_URL_CDN}" ./scripts/deploy/update-manifest-seal-urls.sh
    log_success "Manifest templates updated with CDN URLs"
elif [ -n "${CDN_BASE_URL_BLOB:-}" ]; then
    CDN_BASE_URL="${CDN_BASE_URL_BLOB}" ./scripts/deploy/update-manifest-seal-urls.sh
    log_success "Manifest templates updated with Blob Storage URLs"
    log_warning "CDN endpoint may still be provisioning. Update URLs later when CDN is ready."
else
    log_warning "CDN URLs not available, skipping manifest update"
fi
echo ""

# Step 7: Verify setup
log_info "Step 7: Verifying setup..."
if [ -n "${AZURE_STORAGE_ACCOUNT:-}" ]; then
    TEST_URL="${CDN_BASE_URL_BLOB}digital-bank-seal.png"
    if curl -s -o /dev/null -w "%{http_code}" "${TEST_URL}" | grep -q "200"; then
        log_success "Files are accessible at: ${TEST_URL}"
    else
        log_warning "Files may not be accessible yet (CDN may still be provisioning)"
    fi
fi
echo ""

# Summary
log_info "=== Setup Complete ==="
echo ""
log_success "Azure CDN infrastructure created"
log_success "Credential seal images uploaded"
log_success "Manifest templates updated"
echo ""
log_info "Configuration saved in: azure-cdn-config.env"
log_info "CDN URLs:"
if [ -n "${CDN_BASE_URL_BLOB:-}" ]; then
    echo "  Blob Storage: ${CDN_BASE_URL_BLOB}"
fi
if [ -n "${CDN_BASE_URL_CDN:-}" ] && [ "${CDN_BASE_URL_CDN}" != "https://.azureedge.net/images/" ]; then
    echo "  CDN: ${CDN_BASE_URL_CDN}"
    echo ""
    log_info "Note: CDN endpoint may take 10-15 minutes to fully propagate"
fi
echo ""
log_success "Azure CDN setup complete!"

