#!/bin/bash
set -euo pipefail

OUTPUT_SOURCE="$HOME/ComfyUI/output"
VIDEO_SOURCE="$HOME/ComfyUI/videos"
AUDIO_SOURCE="$HOME/ComfyUI/output/audio"
NAS_MOUNT="/mini.nas/miniShare1"
SHARE_DIR="$NAS_MOUNT/Pictures"
STATE_FILE="$HOME/.local/state/comfyui-sync.state"
NEATCLI="$(command -v /home/linuxbrew/.linuxbrew/bin/neatcli || true)"
RENAME_SCRIPT="$HOME/git/schmeeve-toolz/rename-ai-snaps"
ORGANIZE_SCRIPT="$HOME/git/schmeeve-toolz/organize-images"

mkdir -p "$(dirname "$STATE_FILE")"

if [ ! -d "$SHARE_DIR" ]; then
  echo "[comfyui-sync] ERROR: Share dir $SHARE_DIR does not exist"
  exit 1
fi

if ! mountpoint -q "$NAS_MOUNT"; then
  echo "[comfyui-sync] $NAS_MOUNT is not mounted, attempting to mount..."
  if ! sudo -n mount "$NAS_MOUNT" 2>&1; then
    echo "[comfyui-sync] ERROR: mount attempt failed"
    exit 1
  fi
  if ! mountpoint -q "$NAS_MOUNT"; then
    echo "[comfyui-sync] ERROR: $NAS_MOUNT still not mounted after mount attempt, refusing to copy locally"
    exit 1
  fi
  echo "[comfyui-sync] Mount succeeded."
fi

echo "[comfyui-sync] Starting at $(date)"

current_files=$( {
  find "$OUTPUT_SOURCE" -maxdepth 1 -type f -printf '%f\n' 2>/dev/null
  find "$VIDEO_SOURCE" -maxdepth 1 -type f -printf '%f\n' 2>/dev/null
  find "$AUDIO_SOURCE" -maxdepth 1 -type f -printf '%f\n' 2>/dev/null
} | sort)

if [ -f "$STATE_FILE" ]; then
  new_files=$(comm -13 <(sort "$STATE_FILE") <(echo "$current_files"))
else
  echo "[comfyui-sync] First run — all files considered new."
  new_files="$current_files"
fi

new_count=$(echo "$new_files" | grep -c '[^[:space:]]' || true)

if [ "$new_count" -gt 0 ]; then
  echo "[comfyui-sync] Copying $new_count new file(s)..."
  while IFS= read -r f; do
    [ -z "$f" ] && continue
    if [ -f "$OUTPUT_SOURCE/$f" ]; then
      cp "$OUTPUT_SOURCE/$f" "$SHARE_DIR/"
    elif [ -f "$VIDEO_SOURCE/$f" ]; then
      cp "$VIDEO_SOURCE/$f" "$SHARE_DIR/"
    elif [ -f "$AUDIO_SOURCE/$f" ]; then
      cp "$AUDIO_SOURCE/$f" "$SHARE_DIR/"
    fi
  done <<<"$new_files"

  echo "$current_files" >"$STATE_FILE"
  echo "[comfyui-sync] State file updated."
else
  echo "[comfyui-sync] No new files to copy."
fi

if [ -n "$NEATCLI" ]; then
  echo "[comfyui-sync] Running neatcli organize --by-type -e $SHARE_DIR"
  "$NEATCLI" organize --by-type -e "$SHARE_DIR"
else
  echo "[comfyui-sync] WARNING: neatcli not found, skipping organize step"
fi

IMAGES_DIR="$SHARE_DIR/Images"
if [ -d "$IMAGES_DIR" ] && [ -x "$RENAME_SCRIPT" ]; then
  echo "[comfyui-sync] Running rename-ai-snaps on $IMAGES_DIR"
  cd "$IMAGES_DIR" && "$RENAME_SCRIPT" . --no-interactive
elif [ ! -x "$RENAME_SCRIPT" ]; then
  echo "[comfyui-sync] WARNING: rename-ai-snaps not found, skipping rename step"
fi

if [ -d "$IMAGES_DIR" ] && [ -x "$ORGANIZE_SCRIPT" ]; then
  echo "[comfyui-sync] Running organize-images on $IMAGES_DIR"
  "$ORGANIZE_SCRIPT" "$IMAGES_DIR" --execute
elif [ ! -x "$ORGANIZE_SCRIPT" ]; then
  echo "[comfyui-sync] WARNING: organize-images not found, skipping organize step"
fi

echo "[comfyui-sync] Done at $(date)"
