#!/bin/bash
#
# @author    Assistant
# @version   1.0
# @license   MIT
#
# Script to manage nginx configuration changes, stop harbor, and run a custom script.
# Logs all operations to /root/wdd/ccc.log with detailed levels.
#
# Dependencies:
# - Required commands: mv, systemctl, docker-compose, bash
# - File paths: /etc/nginx/conf.d/, /root/wdd/harbor/docker-compose.yml, /root/wdd/ccc.sh
# - Permissions: root access for file operations and systemctl
#
# Global Constants
readonly LOG_FILE="/root/wdd/ccc.log"

# Error handling: exit on error, unset variable, pipe failure; trap signals
set -euo pipefail
trap 'error_handler' EXIT ERR INT TERM

###
# Logs a message with timestamp and level to both the log file and stderr.
# @param level string The log level (DEBUG, INFO, WARN, ERROR)
# @param message string The message to log
# @return void
# @require date, tee commands
###
log() {
  local level="$1"
  local message="$2"
  local timestamp
  timestamp=$(date '+%Y-%m-%d %H:%M:%S')
  echo "[$timestamp] [$level] $message" | tee -a "$LOG_FILE" >&2
}

###
# Handles errors during script execution, logs error, and exits.
# @return void
###
error_handler() {
  local exit_code=$?
  log ERROR "Script terminated with exit code $exit_code"
  exit $exit_code
}

###
# Moves a file from source to destination with logging.
# @param source_path string Path to the source file
# @param dest_path string Path to the destination file
# @return 0 on success, non-zero on failure
# @require mv command
###
move_file() {
  local source_path="$1"
  local dest_path="$2"
  log INFO "Moving file: $source_path to $dest_path"
  # > Check if source exists before moving
  if [[ ! -f "$source_path" ]]; then
    log WARN "Source file $source_path does not exist, skipping move"
    return 1
  fi
  mv "$source_path" "$dest_path"
  log INFO "File moved successfully: $source_path to $dest_path"
  return 0
}

###
# Restarts the nginx service with systemctl.
# @return 0 on success, non-zero on failure
# @require systemctl command, nginx service
###
restart_nginx() {
  log INFO "Restarting nginx service"
  systemctl restart nginx
  log INFO "Nginx restarted successfully"
  return 0
}

###
# Restarts the nginx service with systemctl.
# @return 0 on success, non-zero on failure
# @require systemctl command, nginx service
###
stop_ingress_expose() {
  log INFO "Stop Ingress Exposition!"
  kubectl delete -f /root/wdd/install/k8s-ingress-nginx.yaml
  log INFO "Stop Ingress Exposition Success！"
  return 0
}

###
# Stops the harbor stack using docker-compose down.
# @return 0 on success, non-zero on failure
# @require docker-compose command, /root/wdd/harbor/docker-compose.yml
###
stop_harbor() {
  local compose_file="/root/wdd/harbor/docker-compose.yml"
  log INFO "Stopping harbor stack with docker-compose"
  # > Check if docker-compose file exists
  if [[ ! -f "$compose_file" ]]; then
    log ERROR "Docker-compose file $compose_file not found"
    return 1
  fi
  docker-compose -f "$compose_file" down -v
  log INFO "Harbor stack stopped successfully"
  return 0
}

###
# Executes a bash script with logging.
# @param script_path string Path to the script to execute
# @return Exit code of the executed script
# @require bash command
###
run_script() {
  local script_path="$1"
  log INFO "Executing script: $script_path"
  # > Check if script exists and is executable
  if [[ ! -f "$script_path" ]]; then
    log ERROR "Script $script_path not found"
    return 1
  fi
  if [[ ! -x "$script_path" ]]; then
    log WARN "Script $script_path is not executable, attempting to run with bash"
  fi
  bash "$script_path"
  local script_exit_code=$?
  log INFO "Script executed with exit code: $script_exit_code"
  return $script_exit_code
}

###
# Main function orchestrating the entire process.
# @return void
###
main() {
  log INFO "Starting main script execution"

  # Move first configuration file
  move_file "/etc/nginx/conf.d/real-proxy.conf" "/etc/nginx/conf.d/real-proxy.conf_back"

  # Move second configuration file
  move_file "/etc/nginx/conf.d/offline.conf_back" "/etc/nginx/conf.d/offline.conf"

  # Restart nginx service
  restart_nginx

  # Stop harbor stack
  stop_harbor

  # Execute the custom script
  run_script "/root/wdd/ccc.sh"

  log INFO "Main script completed successfully"
}

# Function Call Graph:
# main
#   -> move_file (for real-proxy.conf)
#   -> move_file (for offline.conf_back)
#   -> restart_nginx
#   -> stop_harbor
#   -> run_script

# Execute main function
main "$@"
