#!/bin/bash
#
# Phase 15: Production Hardening
# Resource limits, backups, disaster recovery, documentation
#

set -euo pipefail

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

log_info "=========================================="
log_info "Phase 15: Production Hardening"
log_info "=========================================="

# 15.1 Production Configuration
log_step "15.1 Configuring production settings..."

if [ "${ENVIRONMENT}" != "prod" ]; then
  log_warning "Not production environment. Skipping production hardening."
  exit 0
fi

# Update replica counts
log_info "Updating replica counts for production..."
for service in "${SERVICES[@]}"; do
  kubectl scale deployment "${service}" \
    --replicas=3 \
    -n "${NAMESPACE}" \
    || log_warning "Failed to scale ${service}"
done

# 15.2 Backup Configuration
log_step "15.2 Configuring backups..."

# Database backups
log_info "Configuring database backups..."
az postgres server backup create \
  --resource-group "${AKS_RESOURCE_GROUP}" \
  --server-name "${POSTGRES_SERVER_NAME}" \
  --backup-name "daily-backup-$(date +%Y%m%d)" \
  || log_warning "Failed to create database backup"

# Storage backups
log_info "Enabling storage versioning..."
az storage account blob-service-properties update \
  --account-name "${STORAGE_ACCOUNT_NAME}" \
  --enable-versioning true \
  || log_warning "Failed to enable versioning"

log_info "Production hardening complete"
log_info "Next steps (manual):"
log_info "  1. Configure resource limits in deployments"
log_info "  2. Set up automated backups"
log_info "  3. Configure disaster recovery"
log_info "  4. Review security settings"
log_info "  5. Update documentation"

# Save state
save_state "phase15" "complete"

log_success "=========================================="
log_success "Phase 15: Production Hardening - COMPLETE"
log_success "=========================================="

