#!/bin/bash

# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

echo -e "${YELLOW}Running diagnostic checks for Open Productive Stack...${NC}"

# Function to check if a service is running
check_service() {
  local service=$1

  echo -e "${YELLOW}Checking if $service is running...${NC}"
  if docker ps | grep -q "$service"; then
    echo -e "${GREEN}$service is running.${NC}"
    return 0
  else
    echo -e "${RED}$service is not running.${NC}"
    return 1
  fi
}

# Function to check network connectivity
check_connectivity() {
  local service=$1
  local port=$2
  local host=${3:-localhost}

  echo -e "${YELLOW}Checking connectivity to $service on $host:$port...${NC}"
  if nc -z -v -w5 "$host" "$port" 2>/dev/null; then
    echo -e "${GREEN}Connection to $service on $host:$port successful.${NC}"
    return 0
  else
    echo -e "${RED}Cannot connect to $service on $host:$port.${NC}"
    return 1
  fi
}

# Function to check Traefik configuration
check_traefik() {
  echo -e "${YELLOW}Checking Traefik configuration...${NC}"

  # Check if Traefik is running
  check_service "traefik" || return 1

  # Check Traefik ports
  check_connectivity "Traefik HTTP" 80 || echo -e "${RED}Traefik HTTP port not accessible.${NC}"
  check_connectivity "Traefik HTTPS" 443 || echo -e "${RED}Traefik HTTPS port not accessible.${NC}"
  check_connectivity "Traefik SSH" 2424 || echo -e "${RED}Traefik SSH port not accessible.${NC}"

  # Check Traefik certificates
  echo -e "${YELLOW}Checking Traefik certificates...${NC}"
  if docker exec traefik ls -la /certificates/acme.json >/dev/null 2>&1; then
    echo -e "${GREEN}Traefik certificates found.${NC}"
  else
    echo -e "${RED}Traefik certificates not found.${NC}"
  fi

  return 0
}

# Function to check GitLab configuration
check_gitlab() {
  echo -e "${YELLOW}Checking GitLab configuration...${NC}"

  # Check if GitLab is running
  check_service "gitlab" || return 1

  # Check GitLab HTTP port
  docker exec gitlab grep -q "external_url" /etc/gitlab/gitlab.rb && \
    echo -e "${GREEN}GitLab external URL is configured.${NC}" || \
    echo -e "${RED}GitLab external URL is not configured.${NC}"

  # Check GitLab SSH port
  docker exec gitlab grep -q "gitlab_shell_ssh_port" /etc/gitlab/gitlab.rb && \
    echo -e "${GREEN}GitLab SSH port is configured.${NC}" || \
    echo -e "${RED}GitLab SSH port is not configured.${NC}"

  # Check GitLab SSH connection
  echo -e "${YELLOW}Checking GitLab SSH connection...${NC}"
  if ssh -T git@gitlab.${DOMAIN} -p 2424 -o StrictHostKeyChecking=no -o BatchMode=yes &>/dev/null; then
    echo -e "${GREEN}GitLab SSH connection successful.${NC}"
  else
    echo -e "${RED}GitLab SSH connection failed. This is expected if you haven't set up SSH keys yet.${NC}"
    echo -e "${YELLOW}Try: ssh -vT git@gitlab.${DOMAIN} -p 2424${NC}"
  fi

  return 0
}

# Function to check database services
check_databases() {
  echo -e "${YELLOW}Checking database services...${NC}"

  # Check MariaDB
  check_service "mariadb" && \
    echo -e "${GREEN}MariaDB is running.${NC}" || \
    echo -e "${RED}MariaDB is not running.${NC}"

  # Check PostgreSQL
  check_service "postgres" && \
    echo -e "${GREEN}PostgreSQL is running.${NC}" || \
    echo -e "${RED}PostgreSQL is not running.${NC}"

  return 0
}

# Function to check all other services
check_all_services() {
  echo -e "${YELLOW}Checking all services...${NC}"

  local services=("traefik" "gitlab" "mariadb" "postgres" "adminer" "nextcloud" "onlyoffice" "openproject" "hedgedoc" "drupal")

  for service in "${services[@]}"; do
    check_service "$service"
  done

  return 0
}

# Check Docker and Docker Compose
echo -e "${YELLOW}Checking Docker and Docker Compose installation...${NC}"
if command -v docker >/dev/null 2>&1; then
  echo -e "${GREEN}Docker is installed: $(docker --version)${NC}"
else
  echo -e "${RED}Docker is not installed!${NC}"
  exit 1
fi

if docker compose version >/dev/null 2>&1; then
  echo -e "${GREEN}Docker Compose plugin is installed: $(docker compose version)${NC}"
else
  echo -e "${RED}Docker Compose plugin is not installed!${NC}"
  exit 1
fi

# Check system resources
echo -e "${YELLOW}Checking system resources...${NC}"
echo -e "${YELLOW}CPU:${NC} $(grep -c processor /proc/cpuinfo) cores"
echo -e "${YELLOW}Memory:${NC} $(free -h | grep Mem | awk '{print $2}')"
echo -e "${YELLOW}Disk space:${NC} $(df -h / | awk 'NR==2 {print $2}')"

# Domain configuration from .env file
if [ -f "./core/.env" ]; then
  source ./core/.env
  echo -e "${YELLOW}Domain configuration:${NC} ${DOMAIN}"
else
  echo -e "${RED}Core .env file not found!${NC}"
  DOMAIN="example.com"
fi

# Run specific checks
check_traefik
echo ""
check_gitlab
echo ""
check_databases
echo ""
check_all_services

echo -e "${GREEN}Diagnostic checks completed.${NC}"
echo -e "${YELLOW}For detailed logs, run: docker logs <container_name>${NC}"
