#!/bin/bash

set -e
set -o pipefail

FORCE=false

# Parse arguments
while [[ $# -gt 0 ]]; do
    case $1 in
        --force)
            FORCE=true
            shift
            ;;
        --dry-run)
            DRY_RUN="--dry-run=client"
            shift
            ;;
        -h|--help)
            echo "Usage: $0 <app_name> [--force] [--dry-run]"
            echo ""
            echo "Delete a Wild Cloud app and all its resources."
            echo ""
            echo "Options:"
            echo "  --force     Skip confirmation prompts"
            echo "  --dry-run   Show what would be deleted without actually deleting"
            echo "  -h, --help  Show this help message"
            echo ""
            echo "This will delete:"
            echo "  - App deployment, services, and other Kubernetes resources"
            echo "  - App secrets from the app's namespace"
            echo "  - App namespace (if empty after resource deletion)"
            echo "  - Local app configuration files from apps/<app_name>"
            exit 0
            ;;
        -*)
            echo "Unknown option $1"
            echo "Usage: $0 <app_name> [--force] [--dry-run]"
            exit 1
            ;;
        *)
            if [ -z "${APP_NAME}" ]; then
                APP_NAME="$1"
            else
                echo "Too many arguments"
                echo "Usage: $0 <app_name> [--force] [--dry-run]"
                exit 1
            fi
            shift
            ;;
    esac
done

if [ -z "${APP_NAME}" ]; then
    echo "Usage: $0 <app_name> [--force] [--dry-run]"
    exit 1
fi

if [ ! -d "apps/${APP_NAME}" ]; then
    echo "Error: App directory 'apps/${APP_NAME}' not found"
    exit 1
fi

# Initialize Wild Cloud environment
if [ -z "${WC_ROOT}" ]; then
    print "WC_ROOT is not set."
    exit 1
else
    source "${WC_ROOT}/scripts/common.sh"
    init_wild_env
fi

# Confirmation prompt (unless --force or --dry-run)
if [ "${FORCE}" != true ] && [ "${DRY_RUN:-}" != "--dry-run=client" ]; then
    echo "WARNING: This will delete all resources for app '${APP_NAME}'"
    echo "This includes:"
    echo "  - Kubernetes deployments, services, secrets, and other resources"
    echo "  - App namespace (if empty after deletion)"
    echo "  - Local configuration files in apps/${APP_NAME}/"
    echo ""
    read -p "Are you sure you want to delete app '${APP_NAME}'? (y/N): " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        echo "Deletion cancelled"
        exit 1
    fi
fi

echo "Deleting app '${APP_NAME}'"

# Step 1: Delete namespace (this will delete ALL resources including deployments, services, secrets, jobs, PVCs, etc.)
echo "Deleting namespace and all remaining resources..."
kubectl delete namespace "${APP_NAME}" ${DRY_RUN:-} --ignore-not-found=true

# Wait for namespace deletion to complete
if [ "${DRY_RUN:-}" != "--dry-run=client" ]; then
    echo "Waiting for namespace deletion to complete..."
    kubectl wait --for=delete namespace "${APP_NAME}" --timeout=60s || true
fi

# Step 2: Delete local app configuration files
echo "Deleting local app configuration..."
if [ "${DRY_RUN:-}" = "--dry-run=client" ]; then
    echo "DRY RUN: Would delete directory 'apps/${APP_NAME}/'"
else
    rm -rf "apps/${APP_NAME}/"
    echo "Deleted local configuration directory: apps/${APP_NAME}/"
fi

echo "App '${APP_NAME}' deletion complete!"
echo ""
echo "Note: Dependency apps (if any) were not deleted."
echo "If you want to delete dependencies, run wild-app-delete for each dependency separately."