#!/bin/bash

# Define the backup directory and the SQLite database file
BACKUP_DIR="database/backups"
DB_FILE="database/database.sqlite"
# DATE="20260124_115644"
DATE="20260124_123340"

# Ensure the DATE variable is set
if [ -z "$DATE" ]; then
  echo "DATE variable is not set. Please set the DATE variable to match the backup file date."
  exit 1
fi

# Check if the backup directory exists
if [ ! -d "$BACKUP_DIR" ]; then
  echo "Backup directory does not exist: $BACKUP_DIR"
  exit 1
fi

# Check if the SQLite database file exists
if [ ! -f "$DB_FILE" ]; then
  echo "SQLite database file does not exist: $DB_FILE"
  exit 1
fi

# Check if there are any matching backup files
if ! ls "$BACKUP_DIR"/*"$DATE".sql 1> /dev/null 2>&1; then
  echo "No backup files found for the date: $DATE"
  exit 1
fi

echo "Starting restore process from backups dated: $DATE"
echo "Using database file: $DB_FILE"

# Loop through each file in the backup directory
for file in "$BACKUP_DIR"/*"$DATE".sql; do
  if [ -f "$file" ]; then
    echo "Restoring data from $file into $DB_FILE..."

    # Import the SQL file into the SQLite database and log output
    sqlite3 "$DB_FILE" < "$file" 2>> restore_errors.log

    # Check for errors in the restore process
    if [ $? -eq 0 ]; then
      echo "Successfully restored $file"
    else
      echo "Failed to restore $file. Check restore_errors.log for details."
    fi

    # Debugging: Print the last 10 lines of the database to verify data
    # echo "Last 10 rows of the database after restoring $file:"
    # sqlite3 "$DB_FILE" "SELECT * FROM sqlite_master WHERE type='table';" 2>> restore_errors.log
    # sqlite3 "$DB_FILE" "SELECT * FROM entries ORDER BY rowid DESC LIMIT 10;" 2>> restore_errors.log
  fi
done

echo "Restore process completed."