#!/bin/bash
# Complete Entra VerifiedID Setup - Master Script
# Orchestrates all setup steps in the correct order

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"; }
log_step() { echo -e "\n${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}\n${BLUE}Step $1:${NC} $2\n${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}\n"; }

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

log_info "Entra VerifiedID Complete Setup"
log_info "This script will guide you through all setup steps"
echo ""

# Step 1: Azure App Registration
log_step "1" "Azure AD App Registration"
read -p "Have you created the Azure AD App Registration? (y/n): " APP_REG_DONE
if [ "${APP_REG_DONE}" != "y" ]; then
    log_info "Running app registration script..."
    ./scripts/deploy/create-entra-app.sh
else
    log_success "App registration already done"
fi

# Step 2: API Permissions
log_step "2" "API Permissions Configuration"
read -p "Have you configured API permissions? (y/n): " PERMS_DONE
if [ "${PERMS_DONE}" != "y" ]; then
    log_info "Running API permissions configuration..."
    ./scripts/deploy/configure-api-permissions.sh
else
    log_success "API permissions already configured"
fi

# Step 3: Enable Verified ID
log_step "3" "Enable Verified ID Service"
read -p "Is Verified ID service enabled? (y/n): " VERIFIED_ID_DONE
if [ "${VERIFIED_ID_DONE}" != "y" ]; then
    log_info "Running Verified ID enablement guide..."
    ./scripts/deploy/enable-verified-id.sh
    read -p "Press Enter after enabling Verified ID service..."
else
    log_success "Verified ID service already enabled"
fi

# Step 4: Create Manifests
log_step "4" "Create Credential Manifests"
read -p "Have you created credential manifests? (y/n): " MANIFESTS_DONE
if [ "${MANIFESTS_DONE}" != "y" ]; then
    log_info "Running manifest creation guide..."
    ./scripts/deploy/create-credential-manifests.sh
    read -p "Press Enter after creating manifests and collecting Manifest IDs..."
    ./manifests/entra/collect-manifest-ids.sh
else
    log_success "Manifests already created"
fi

# Step 5: Store Secrets
log_step "5" "Store Secrets in Key Vault"
read -p "Have you stored secrets in Key Vault? (y/n): " SECRETS_DONE
if [ "${SECRETS_DONE}" != "y" ]; then
    log_info "Running secret storage script..."
    ./scripts/deploy/store-entra-secrets.sh
else
    log_success "Secrets already stored"
fi

# Step 6: Environment Configuration
log_step "6" "Configure Environment"
read -p "Configure development environment? (y/n): " CONFIG_ENV
if [ "${CONFIG_ENV}" = "y" ]; then
    ./scripts/deploy/configure-env-dev.sh
fi

# Step 7: Multi-Manifest (if applicable)
log_step "7" "Configure Multi-Manifest Support"
read -p "Do you have multiple manifests to configure? (y/n): " MULTI_MANIFEST
if [ "${MULTI_MANIFEST}" = "y" ]; then
    ./scripts/deploy/configure-multi-manifest.sh
fi

# Step 8: Validation
log_step "8" "Validate Configuration"
log_info "Running validation..."
if ./scripts/validation/validate-entra-config.sh; then
    log_success "Configuration validated"
else
    log_warning "Validation found issues. Please review and fix."
fi

# Step 9: Testing
log_step "9" "Run Tests"
read -p "Run unit tests? (y/n): " RUN_UNIT
if [ "${RUN_UNIT}" = "y" ]; then
    pnpm --filter @the-order/auth test entra-verifiedid.test.ts --run
fi

read -p "Run integration tests? (requires credentials) (y/n): " RUN_INTEGRATION
if [ "${RUN_INTEGRATION}" = "y" ]; then
    ./scripts/test/run-integration-tests-with-setup.sh
fi

# Step 10: Deployment
log_step "10" "Deployment"
read -p "Deploy to staging? (y/n): " DEPLOY_STAGING
if [ "${DEPLOY_STAGING}" = "y" ]; then
    ./scripts/deploy/deploy-staging.sh
fi

read -p "Configure webhook URL? (y/n): " CONFIG_WEBHOOK
if [ "${CONFIG_WEBHOOK}" = "y" ]; then
    ./scripts/deploy/configure-webhook-url.sh
fi

# Summary
echo ""
log_success "Setup Complete!"
echo ""
log_info "Next steps:"
echo "1. Verify staging deployment"
echo "2. Test credential issuance"
echo "3. Monitor metrics"
echo "4. Deploy to production when ready"
echo ""
log_info "For detailed information, see:"
echo "  - docs/deployment/ENTRA_VERIFIEDID_DEPLOYMENT_CHECKLIST.md"
echo "  - docs/operations/ENTRA_VERIFIEDID_RUNBOOK.md"

