#!/bin/bash

# Setup Local Backup Environment
# Creates a local backup directory structure for testing the web dashboard

BACKUP_BASE_DIR="$HOME/shell-backups"
METRICS_DIR="$BACKUP_BASE_DIR/metrics"

echo "Setting up local backup environment at: $BACKUP_BASE_DIR"

# Create directory structure
mkdir -p "$BACKUP_BASE_DIR"/{plex,immich,media-services}/{scheduled,manual}
mkdir -p "$METRICS_DIR"

# Copy existing metrics files if they exist
if [[ -d "/home/acedanger/shell/metrics" ]]; then
    cp /home/acedanger/shell/metrics/*.json "$METRICS_DIR/" 2>/dev/null || true
fi

# Create sample backup files with realistic names and sizes
echo "Creating sample backup files..."

# Plex backups
echo "Sample Plex database backup content" > "$BACKUP_BASE_DIR/plex/scheduled/plex-db-backup-$(date +%Y%m%d-%H%M%S).tar.gz"
echo "Sample Plex config backup content" > "$BACKUP_BASE_DIR/plex/manual/plex-config-$(date +%Y%m%d).zip"

# Immich backups  
echo "Sample Immich database dump" > "$BACKUP_BASE_DIR/immich/immich-database-$(date +%Y%m%d).sql"
echo "Sample Immich assets backup" > "$BACKUP_BASE_DIR/immich/scheduled/immich-assets-$(date +%Y%m%d).tar.gz"

# Media services backups
echo "Sample media services configuration" > "$BACKUP_BASE_DIR/media-services/media-services-config-$(date +%Y%m%d).json"

# Make files larger to simulate real backups (optional)
if command -v fallocate >/dev/null 2>&1; then
    fallocate -l 1M "$BACKUP_BASE_DIR/plex/scheduled/plex-db-backup-$(date +%Y%m%d-%H%M%S).tar.gz"
    fallocate -l 500K "$BACKUP_BASE_DIR/immich/immich-database-$(date +%Y%m%d).sql"
fi

echo "Local backup environment setup complete!"
echo "Backup directory: $BACKUP_BASE_DIR"
echo "To use with web app: export BACKUP_ROOT=\"$BACKUP_BASE_DIR\""
echo ""
echo "Contents:"
find "$BACKUP_BASE_DIR" -type f | head -10
