#!/usr/bin/env bash
# Azure CLI wrapper functions
# Usage: source "$SCRIPT_DIR/lib/azure/cli.sh"

# Source logging if not already sourced
[ -z "${log_error:-}" ] && source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../common/logging.sh"

# Check if Azure CLI is installed
check_azure_cli() {
    if ! command -v az &> /dev/null; then
        log_error "Azure CLI not found. Please install Azure CLI."
        log_info "Visit: https://docs.microsoft.com/cli/azure/install-azure-cli"
        return 1
    fi
    return 0
}

# Check if logged in to Azure
check_azure_login() {
    if ! az account show &> /dev/null; then
        log_error "Not logged in to Azure. Run 'az login' first."
        return 1
    fi
    return 0
}

# Ensure Azure CLI is ready (checks installation and login)
ensure_azure_cli() {
    check_azure_cli || return 1
    check_azure_login || return 1
    
    # Set subscription if configured
    local subscription_id="${AZURE_SUBSCRIPTION_ID:-}"
    if [ -n "$subscription_id" ]; then
        az account set --subscription "$subscription_id" &> /dev/null || true
    fi
    
    return 0
}

# Get current subscription ID
get_current_subscription() {
    az account show --query id -o tsv 2>/dev/null
}

# Get current subscription name
get_current_subscription_name() {
    az account show --query name -o tsv 2>/dev/null
}

# List all subscriptions
list_subscriptions() {
    az account list --query "[].{Name:name, ID:id, IsDefault:isDefault}" -o table
}

