#!/bin/bash
# export_migration_data.sh
# Script to export database tables and product images into a template.
# Run this on the host machine where the docker containers are running.

echo "Starting Migration Data Export..."

TEMPLATE_DIR="database/seeders/template_data"
SQL_FILE="${TEMPLATE_DIR}/migration_data.sql"
IMAGES_DIR="${TEMPLATE_DIR}/images"

mkdir -p "${TEMPLATE_DIR}"

echo "1/2: Exporting database tables from gembira-db container..."
TABLES="users suppliers warehouses categories products warehouse_stock"

# Using --replace so it safely overwrites on import
docker exec gembira-db mysqldump -ugembira_user -pchangeme --replace --no-create-info --skip-triggers --compact gembira_ims $TABLES > "${SQL_FILE}"

if [ $? -eq 0 ]; then
    echo "Database tables exported successfully to ${SQL_FILE}"
else
    echo "Error exporting database tables."
fi

echo "2/2: Copying product images from gembira-backend container..."
mkdir -p "${IMAGES_DIR}"
docker cp gembira-backend:/app/storage/app/public/products/. "${IMAGES_DIR}/" 2>/dev/null
echo "Product images copied to ${IMAGES_DIR}"

echo "Export Completed! You can commit the '${TEMPLATE_DIR}' folder to git to migrate to a new server."
