#!/bin/bash
# services/control-plane/deploy-local.sh
set -e

# 1. Validate it is deploying control-plane
if [[ ! $(pwd) == *"/services/control-plane" ]]; then
    echo "Error: Script must be run from services/control-plane directory"
    exit 1
fi

if [[ ! -f "docker-compose.yml" ]]; then
    echo "Error: docker-compose.yml not found"
    exit 1
fi

echo "--- Preparing Control Plane Directories ---"
# 2. Prepare required dirs
# /opt/homelab/config
# /opt/homelab/actions/{pending,approved,rejected,running,completed,failed}
# /opt/homelab/world
# /opt/homelab/state

DIRS=(
    "/opt/homelab/config"
    "/opt/homelab/actions/pending"
    "/opt/homelab/actions/approved"
    "/opt/homelab/actions/rejected"
    "/opt/homelab/actions/running"
    "/opt/homelab/actions/completed"
    "/opt/homelab/actions/failed"
    "/opt/homelab/world"
    "/opt/homelab/state"
)

for dir in "${DIRS[@]}"; do
    if [ ! -d "$dir" ]; then
        echo "Creating $dir"
        sudo mkdir -p "$dir"
    fi
done

# 3. chown/chmod for UID 1000 — self-healing: only calls sudo when actually needed
echo "Checking /opt/homelab ownership..."
_chown_needed=$(find /opt/homelab \( ! -uid 1000 -o ! -gid 1000 \) -print -quit 2>/dev/null)
if [[ -n "$_chown_needed" ]]; then
    echo "Found files not owned by 1000:1000 (e.g. $_chown_needed) — fixing..."
    sudo chown -R 1000:1000 /opt/homelab
else
    echo "Ownership already correct, skipping chown"
fi

echo "Checking /opt/homelab directory permissions..."
_chmod_needed=$(find /opt/homelab -type d ! -perm -775 -print -quit 2>/dev/null)
if [[ -n "$_chmod_needed" ]]; then
    echo "Found directories with wrong permissions (e.g. $_chmod_needed) — fixing..."
    sudo chmod -R 775 /opt/homelab 2>/dev/null || true
else
    echo "Permissions already correct, skipping chmod"
fi

# 4. Run docker compose up -d --build --force-recreate
echo "--- Starting Control Plane Services ---"
COMPOSE_ARGS="-f docker-compose.yml"
OVERRIDE_FILE="../../hosts/vps/runtime/control-plane/docker-compose.override.yml"
if [ -f "$OVERRIDE_FILE" ]; then
    echo "Using override: $OVERRIDE_FILE"
    COMPOSE_ARGS="$COMPOSE_ARGS -f $OVERRIDE_FILE"
fi
docker compose $COMPOSE_ARGS up -d --build --force-recreate

# 5. Print docker ps for control-plane containers
echo "--- Deployment Status ---"
docker ps --filter "name=control-plane"
