#!/bin/bash
# Info: Coder preparation script
# Author: Provisioning System
# Release: 1.0

echo "Preparing Coder installation..."

# Load environment variables
[ -r "env-coder" ] && . ./env-coder

# Check if required tools are available
command -v curl >/dev/null 2>&1 || { echo "curl is required but not installed." >&2; exit 1; }
command -v tar >/dev/null 2>&1 || { echo "tar is required but not installed." >&2; exit 1; }
command -v systemctl >/dev/null 2>&1 || { echo "systemctl is required but not installed." >&2; exit 1; }

# Check for Git (recommended for Coder workspaces)
if ! command -v git >/dev/null 2>&1; then
    echo "Warning: Git not found. Git is recommended for Coder workspaces."
fi

# Validate configuration
if [ -z "$CODER_VERSION" ]; then
    echo "CODER_VERSION must be set" >&2
    exit 1
fi

if [ -z "$CODER_ACCESS_URL" ]; then
    echo "CODER_ACCESS_URL must be set" >&2
    exit 1
fi

# Validate access URL format
if ! echo "$CODER_ACCESS_URL" | grep -qE '^https?://'; then
    echo "CODER_ACCESS_URL must be a valid HTTP/HTTPS URL" >&2
    exit 1
fi

# Check if access URL is not localhost for production
if echo "$CODER_ACCESS_URL" | grep -q "localhost\|127\.0\.0\.1"; then
    echo "Warning: Using localhost in CODER_ACCESS_URL. This should only be used for development."
fi

# Check port availability
CODER_PORT=$(echo "$CODER_HTTP_ADDRESS" | sed 's/.*://')
if command -v netstat >/dev/null 2>&1; then
    if netstat -tuln | grep -q ":${CODER_PORT} "; then
        echo "Warning: Port ${CODER_PORT} appears to be in use"
    fi
elif command -v ss >/dev/null 2>&1; then
    if ss -tuln | grep -q ":${CODER_PORT} "; then
        echo "Warning: Port ${CODER_PORT} appears to be in use"
    fi
fi

# Validate database configuration
if [ -n "$CODER_PG_CONNECTION_URL" ]; then
    echo "Using external PostgreSQL database"
    # Basic validation of PostgreSQL URL format
    if ! echo "$CODER_PG_CONNECTION_URL" | grep -qE '^(postgresql|postgres)://'; then
        echo "Invalid PostgreSQL connection URL format" >&2
        exit 1
    fi
else
    echo "Using built-in PostgreSQL database"
fi

# Check TLS configuration if enabled
if [ "${CODER_TLS_ENABLE:-false}" = "true" ]; then
    echo "TLS is enabled"
    if [ -z "$CODER_TLS_CERT_FILE" ] || [ -z "$CODER_TLS_KEY_FILE" ]; then
        echo "TLS enabled but certificate files not specified" >&2
        exit 1
    fi
    
    if [ ! -f "$CODER_TLS_CERT_FILE" ]; then
        echo "Warning: TLS certificate file not found: $CODER_TLS_CERT_FILE"
    fi
    
    if [ ! -f "$CODER_TLS_KEY_FILE" ]; then
        echo "Warning: TLS key file not found: $CODER_TLS_KEY_FILE"
    fi
fi

# Check OAuth configuration if enabled
if [ -n "$CODER_OAUTH2_GITHUB_CLIENT_ID" ] || [ -n "$CODER_OIDC_CLIENT_ID" ] || [ -n "$CODER_OAUTH2_GOOGLE_CLIENT_ID" ]; then
    echo "OAuth authentication is configured"
fi

# Check system resources
echo "Checking system resources..."
FREE_MEMORY=$(free -m 2>/dev/null | awk '/^Mem:/{print $7}' || echo "unknown")
if [ "$FREE_MEMORY" != "unknown" ] && [ "$FREE_MEMORY" -lt 2048 ]; then
    echo "Warning: Less than 2GB of free memory available. Coder recommends at least 4GB for optimal performance."
fi

CPU_CORES=$(nproc 2>/dev/null || echo "unknown")
if [ "$CPU_CORES" != "unknown" ] && [ "$CPU_CORES" -lt 2 ]; then
    echo "Warning: Less than 2 CPU cores available. Coder recommends at least 2 cores for optimal performance."
fi

echo "Preparation completed successfully."