#!/usr/bin/env zsh
# deploy-coding-plan-dashboard-macos26.zsh
# Deploys the coding-plan-dashboard project using Docker Compose.
# Requirements: Docker Engine and Docker Compose v2 must be installed.
# Safe to run multiple times (idempotent).

set -euo pipefail

REPO_URL="https://github.com/margrop/coding-plan-dashboard"
PROJECT_DIR="$HOME/coding-plan-dashboard"
DATA_DIR="$PROJECT_DIR/data"
ACCESS_URL="http://localhost:8080"

# Step 1: Clone or update the repository
if [[ -d "$PROJECT_DIR" ]]; then
    echo "[1/4] Project directory exists, pulling latest changes..."
    git -C "$PROJECT_DIR" pull --ff-only
else
    echo "[1/4] Cloning repository..."
    git clone "$REPO_URL" "$PROJECT_DIR"
fi

# Step 2: Create the data directory with owner-only permissions (chmod 700)
echo "[2/4] Ensuring data directory exists with proper permissions..."
mkdir -p "$DATA_DIR"
chmod 700 "$DATA_DIR"

# Step 3: Start containers with Docker Compose
echo "[3/4] Starting Docker Compose services..."
docker compose -f "$PROJECT_DIR/docker-compose.yml" up -d

# Step 4: Print success message with access URL
echo "[4/4] Deployment complete!"
echo ""
echo "Access the dashboard at: $ACCESS_URL"
echo ""
