#!/bin/bash
# Restore Script - Run after computer reset
# Restores cron jobs, launchd services, configs, and tracking data

if [[ -z "$1" ]]; then
  echo "ERROR: Please specify backup directory"
  echo "Usage: $0 <backup-directory>"
  echo ""
  echo "Example: $0 ~/.clawdbot/workspace/backup-before-reset-20260119-120000"
  exit 1
fi

BACKUP_DIR="$1"

if [[ ! -d "$BACKUP_DIR" ]]; then
  echo "ERROR: Backup directory not found: $BACKUP_DIR"
  exit 1
fi

echo "=========================================="
echo "RESTORE SCRIPT FOR COMPUTER RESET"
echo "=========================================="
echo "Backup location: $BACKUP_DIR"
echo ""

# Verify checksums
echo "Verifying backup integrity..."
cd "$BACKUP_DIR"
if [[ -f "sha256-checksums.txt" ]]; then
  shasum -c sha256-checksums.txt 2>/dev/null
  if [[ $? -eq 0 ]]; then
    echo "   ✓ All files verified"
  else
    echo "   ⚠️  Checksum mismatch - some files may be corrupted"
    read -p "Continue anyway? (y/N) " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
      exit 1
    fi
  fi
else
  echo "   ⚠️  Checksums not found, skipping verification"
fi
echo ""

# 1. Restore crontab
echo "[1/7] Restoring crontab..."
if [[ -f "$BACKUP_DIR/crontab-backup.txt" ]]; then
  crontab "$BACKUP_DIR/crontab-backup.txt"
  echo "   ✓ Restored $(wc -l < "$BACKUP_DIR/crontab-backup.txt") cron jobs"
else
  echo "   ⚠️  crontab-backup.txt not found, skipping"
fi

# 2. Restore launchd services
echo "[2/7] Restoring launchd services..."
if [[ -d "$BACKUP_DIR/launchd" ]]; then
  mkdir -p ~/Library/LaunchAgents/
  cp -R "$BACKUP_DIR/launchd/"* ~/Library/LaunchAgents/ 2>/dev/null

  # Load services
  if [[ -f ~/Library/LaunchAgents/com.jakeshore.remix-sniper.plist ]]; then
    launchctl load -w ~/Library/LaunchAgents/com.jakeshore.remix-sniper.plist 2>/dev/null
    echo "   ✓ Loaded remix-sniper launchd service"
  fi
else
  echo "   ⚠️  launchd directory not found, skipping"
fi

# 3. Restore PostgreSQL database
echo "[3/7] Restoring PostgreSQL database..."
if [[ -f "$BACKUP_DIR/remix_sniper-db.sql" ]]; then
  # Check if PostgreSQL is installed
  if command -v /opt/homebrew/opt/postgresql@16/bin/psql &> /dev/null; then
    /opt/homebrew/opt/postgresql@16/bin/psql -d remix_sniper < "$BACKUP_DIR/remix_sniper-db.sql" 2>/dev/null
    echo "   ✓ Restored database ($(wc -l < "$BACKUP_DIR/remix_sniper-db.sql") lines)"
  else
    echo "   ⚠️  PostgreSQL not installed - install first then run:"
    echo "      /opt/homebrew/opt/postgresql@16/bin/psql -d remix_sniper < \"$BACKUP_DIR/remix_sniper-db.sql\""
  fi
else
  echo "   ⚠️  Database dump not found, skipping"
fi

# 4. Restore Remix Sniper tracking data
echo "[4/7] Restoring Remix Sniper tracking data..."
if [[ -d "$BACKUP_DIR/remix-sniper" ]]; then
  mkdir -p ~/.remix-sniper
  cp -R "$BACKUP_DIR/remix-sniper/"* ~/.remix-sniper/ 2>/dev/null
  echo "   ✓ Restored tracking data ($(find ~/.remix-sniper -type f | wc -l) files)"
else
  echo "   ⚠️  Remix Sniper data not found, skipping"
fi

# 5. Restore environment files
echo "[5/7] Restoring environment files..."
if [[ -d "$BACKUP_DIR/env-files" ]]; then
  mkdir -p ~/projects/remix-sniper/
  cp "$BACKUP_DIR/env-files/.env" ~/projects/remix-sniper/ 2>/dev/null
  echo "   ✓ Restored .env file"
else
  echo "   ⚠️  Environment files not found, skipping"
fi

# 6. Restore Clawdbot workspace
echo "[6/7] Restoring Clawdbot workspace..."
if [[ -d "$BACKUP_DIR/clawdbot-workspace" ]]; then
  mkdir -p ~/.clawdbot/workspace/
  cp -R "$BACKUP_DIR/clawdbot-workspace/"* ~/.clawdbot/workspace/ 2>/dev/null
  echo "   ✓ Restored workspace ($(find ~/.clawdbot/workspace -type f | wc -l) files)"
else
  echo "   ⚠️  Workspace backup not found, skipping"
fi

# 7. Restore scripts
echo "[7/7] Restoring custom scripts..."
if [[ -d "$BACKUP_DIR/scripts" ]]; then
  mkdir -p ~/.clawdbot/workspace/
  cp "$BACKUP_DIR/scripts/"* ~/.clawdbot/workspace/ 2>/dev/null
  chmod +x ~/.clawdbot/workspace/*.sh 2>/dev/null
  echo "   ✓ Restored custom scripts"
else
  echo "   ⚠️  Scripts directory not found, skipping"
fi

echo ""
echo "=========================================="
echo "RESTORE COMPLETE"
echo "=========================================="
echo ""
echo "Next steps:"
echo "  1. Verify crontab: crontab -l"
echo "  2. Check launchd services: launchctl list | grep remix-sniper"
echo "  3. Check PostgreSQL: /opt/homebrew/opt/postgresql@16/bin/psql -d remix_sniper -c '\l'"
echo "  4. Test Remix Sniper bot: Check if bot is online in Discord"
echo ""
echo "Note: You may need to:"
echo "  - Restart PostgreSQL: brew services restart postgresql@16"
echo "  - Restart launchd services: launchctl restart com.jakeshore.remix-sniper"
echo ""
