# gdrive-organizer task runner
#
# Usage: `just <recipe>` — list all recipes with `just`.

set shell := ["bash", "-cu"]

# Web viewer port (override: `just PORT=9000 start`)
PORT := "8765"
PID_FILE := ".viewer.pid"
LOG_FILE := ".viewer.log"
GDO := "uv run gdrive-organizer"

# Default recipe: list everything
default:
    @just --list

# ── Web Viewer ──────────────────────────────────────────

# Start the web viewer in background on port {{PORT}}
start:
    #!/usr/bin/env bash
    set -eu
    if [ -f {{PID_FILE}} ] && kill -0 "$(cat {{PID_FILE}})" 2>/dev/null; then
        echo "Viewer already running (PID $(cat {{PID_FILE}})) on port {{PORT}}"
        exit 0
    fi
    nohup {{GDO}} view --port {{PORT}} > {{LOG_FILE}} 2>&1 &
    echo $! > {{PID_FILE}}
    sleep 1
    echo "Viewer started → http://127.0.0.1:{{PORT}} (PID $(cat {{PID_FILE}}))"

# Run the viewer in foreground (Ctrl+C to stop)
start-fg:
    {{GDO}} view --port {{PORT}}

# Stop the background viewer
stop:
    #!/usr/bin/env bash
    set -eu
    if [ ! -f {{PID_FILE}} ]; then
        echo "Viewer not running"
        exit 0
    fi
    PID=$(cat {{PID_FILE}})
    if kill -0 "$PID" 2>/dev/null; then
        kill "$PID"
        echo "Viewer stopped (PID $PID)"
    else
        echo "Stale PID file removed"
    fi
    rm -f {{PID_FILE}}

# Restart the background viewer
restart: stop start

# Show viewer status
status:
    #!/usr/bin/env bash
    if [ -f {{PID_FILE}} ] && kill -0 "$(cat {{PID_FILE}})" 2>/dev/null; then
        echo "Viewer running (PID $(cat {{PID_FILE}})) on port {{PORT}}"
    else
        echo "Viewer not running"
    fi

# Tail viewer logs
logs:
    tail -f {{LOG_FILE}}

# ── Drive Operations ────────────────────────────────────

# Scan Google Drive and save snapshot — extra args passed through
scan *args:
    {{GDO}} scan {{args}}

# Display Drive file tree
tree *args:
    {{GDO}} tree {{args}}

# Show Drive statistics
stats:
    {{GDO}} stats

# Show permission summary
permissions:
    {{GDO}} permissions

# Classify files using AI preset and generate plan
classify *args:
    {{GDO}} classify {{args}}

# Read file content as text
cat file_id:
    {{GDO}} cat {{file_id}}

# Export a file
export *args:
    {{GDO}} export {{args}}

# Create a new folder
mkdir *args:
    {{GDO}} create-folder {{args}}

# ── Auth ────────────────────────────────────────────────

# Authenticate with Google Drive (OAuth)
login:
    {{GDO}} auth login

# Check authentication status
whoami:
    {{GDO}} auth status

# Remove stored auth token
logout:
    {{GDO}} auth logout

# ── Plan ────────────────────────────────────────────────

# Show the latest execution plan
plan:
    {{GDO}} plan show

# Apply the latest execution plan
apply *args:
    {{GDO}} plan apply {{args}}

# Dry-run the latest plan
dry-run:
    {{GDO}} plan apply --dry-run

# Rollback the last applied plan
rollback:
    {{GDO}} plan rollback

# ── Config / Preset ─────────────────────────────────────

# Show current configuration
config:
    {{GDO}} config show

# Update a configuration value
config-set key value:
    {{GDO}} config set {{key}} {{value}}

# List classification presets
presets:
    {{GDO}} preset list

# Show preset details
preset-show name:
    {{GDO}} preset show {{name}}

# ── Dev Tools ───────────────────────────────────────────

# Run pytest (extra args passed through)
test *args:
    uv run pytest tests/ {{args}}

# Lint with ruff
lint:
    uv run ruff check src/

# Format with ruff
fmt:
    uv run ruff format src/

# Lint + format + test
check: lint fmt test

# Sync dependencies via uv (includes dev extras: pytest, ruff, etc.)
sync:
    uv sync --all-extras
