#!/usr/bin/env bash
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/domains/functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x

cmd-domains-report() {
  declare desc="displays a domains report for one or more apps"
  declare cmd="domains:report"
  [[ "$1" == "$cmd" ]] && shift 1
  declare APP="$1" INFO_FLAG="$2"
  local INSTALLED_APPS=$(dokku_apps)

  if [[ "$APP" == "--global" ]]; then
    cmd-domains-report-single "$APP" "$INFO_FLAG"
    return
  fi

  if [[ -n "$APP" ]] && [[ "$APP" == --* ]]; then
    INFO_FLAG="$APP"
    APP=""
  fi

  if [[ -z "$APP" ]] && [[ -z "$INFO_FLAG" ]]; then
    INFO_FLAG="true"
  fi

  if [[ -z "$APP" ]]; then
    for app in $INSTALLED_APPS; do
      cmd-domains-report-single "$app" "$INFO_FLAG" | tee || true
    done
  else
    cmd-domains-report-single "$APP" "$INFO_FLAG"
  fi
}

cmd-domains-report-single() {
  declare APP="$1" INFO_FLAG="$2"
  if [[ "$INFO_FLAG" == "true" ]]; then
    INFO_FLAG=""
  fi
  local flag_map=() app_flags=() global_flags=()

  if [[ "$APP" != "--global" ]]; then
    verify_app_name "$APP"
    app_flags=(
      "--domains-app-enabled: $(fn-domains-app-enabled "$APP")"
      "--domains-app-vhosts: $(fn-domains-app-vhosts "$APP" | awk '{$1=$1};1')"
    )
  fi

  global_flags=(
    "--domains-global-enabled: $(fn-domains-global-enabled)"
    "--domains-global-vhosts: $(fn-domains-global-vhosts | awk '{$1=$1};1')"
  )

  flag_map=("${app_flags[@]}" "${global_flags[@]}")

  if [[ -z "$INFO_FLAG" ]]; then
    if [[ "$APP" == "--global" ]]; then
      dokku_log_info2_quiet "Global domains information"
    else
      dokku_log_info2_quiet "$APP domains information"
    fi
    for flag in "${flag_map[@]}"; do
      key="$(echo "${flag#--}" | cut -f1 -d' ' | tr - ' ')"
      dokku_log_verbose "$(printf "%-30s %-25s" "${key^}" "${flag#*: }")"
    done
  else
    local match=false
    local value_exists=false
    for flag in "${flag_map[@]}"; do
      valid_flags="${valid_flags} $(echo "$flag" | cut -d':' -f1)"
      if [[ "$flag" == "${INFO_FLAG}:"* ]]; then
        value=${flag#*: }
        size="${#value}"
        if [[ "$size" -ne 0 ]]; then
          echo "$value" && match=true && value_exists=true
        else
          match=true
        fi
      fi
    done
    [[ "$match" == "true" ]] || dokku_log_fail "Invalid flag passed, valid flags:${valid_flags}"
  fi
}

fn-domains-app-enabled() {
  declare APP="$1"
  local DOMAINS_APP_ENABLED=false
  if [[ "$(is_app_vhost_enabled "$APP")" == "true" ]]; then
    DOMAINS_APP_ENABLED=true
  fi
  echo "$DOMAINS_APP_ENABLED"
}

fn-domains-app-vhosts() {
  declare APP="$1"
  local APP_VHOST_PATH="$DOKKU_ROOT/$APP/VHOST"
  if [[ -f "$APP_VHOST_PATH" ]]; then
    tr '\n' ' ' <"$APP_VHOST_PATH"
  fi
}

fn-domains-global-enabled() {
  local DOMAINS_GLOBAL_ENABLED=false
  if [[ "$(is_global_vhost_enabled)" == "true" ]]; then
    DOMAINS_GLOBAL_ENABLED=true
  fi
  echo "$DOMAINS_GLOBAL_ENABLED"
}

fn-domains-global-vhosts() {
  if [[ "$(is_global_vhost_enabled)" == "true" ]]; then
    get_global_vhosts | tr '\n' ' '
  fi
}
