#!/bin/bash
# Add Media Downloader backup profile to Backup Central
# Run this script to create or recreate the backup profile

set -e

echo "Adding Media Downloader backup profile to Backup Central..."
echo ""

# Delete existing profile if it exists
sqlite3 /opt/backup-central/data/backup_cache.db "DELETE FROM backup_profiles WHERE id = 'profile-media-downloader';" 2>/dev/null || true

# Insert new profile
sqlite3 /opt/backup-central/data/backup_cache.db <<'SQL'
INSERT INTO backup_profiles (
    id,
    name,
    description,
    enabled,
    created_at,
    updated_at,
    destination_type,
    destination_path,
    sources,
    schedule_enabled,
    schedule_frequency,
    schedule_time,
    retention_daily,
    retention_weekly,
    retention_monthly,
    retention_yearly,
    notify_on_success,
    notify_on_warning,
    notify_on_failure,
    notify_channels,
    advanced_settings,
    total_runs,
    success_count,
    failure_count
) VALUES (
    'profile-media-downloader',
    'Media Downloader System',
    'Daily backup of media-downloader configuration, database, and code',
    1,
    datetime('now'),
    datetime('now'),
    'local',
    '/media/backups/Ubuntu/restic-repo',
    '{"include":["/opt/media-downloader/data","/opt/media-downloader/database","/opt/media-downloader/cookies","/opt/media-downloader/sessions","/opt/media-downloader/modules","/opt/media-downloader/wrappers","/opt/media-downloader/scripts","/opt/media-downloader/web/backend","/opt/media-downloader/web/frontend/src","/opt/media-downloader/*.py","/opt/media-downloader/VERSION","/opt/media-downloader/README.md","/opt/media-downloader/requirements.txt","/opt/media-downloader/docs"],"exclude":["*.log","*.log.*","*.pyc","__pycache__","/opt/media-downloader/temp/*","/opt/media-downloader/logs/*","/opt/media-downloader/venv/*","/opt/media-downloader/.playwright/*","/opt/media-downloader/debug/*","/opt/media-downloader/database/*.db-shm","/opt/media-downloader/database/*.db-wal","*.swp","*.swo","*~",".*.swp"]}',
    1,
    'daily',
    '00:00',
    7,
    4,
    12,
    2,
    0,
    1,
    1,
    '["pushover"]',
    '{"custom_name_template":"{{version}}-{{datetime}}","auto_lock_all_backups":true}',
    0,
    0,
    0
);
SQL

if [ $? -eq 0 ]; then
    echo "✓ Profile added successfully"
    echo ""

    # Restart backup-central to load the profile
    echo "Restarting Backup Central service..."
    sudo systemctl restart backup-central
    sleep 2

    echo "✓ Backup Central restarted"
    echo ""

    # Verify profile was created
    echo "Verifying profile..."
    backup-central profiles list | grep "Media Downloader"

    echo ""
    echo "╔════════════════════════════════════════════════╗"
    echo "║          Profile Added Successfully            ║"
    echo "╠════════════════════════════════════════════════╣"
    echo "║ ID:       profile-media-downloader             ║"
    echo "║ Name:     Media Downloader System              ║"
    echo "║ Schedule: Daily at 00:00 (midnight)            ║"
    echo "║ Status:   Enabled                              ║"
    echo "╚════════════════════════════════════════════════╝"
    echo ""
    echo "To view full details:"
    echo "  backup-central profiles --info profile-media-downloader"
    echo ""
else
    echo "✗ Failed to add profile"
    exit 1
fi
