#!/bin/bash
# import_migration_data.sh
# Script to import database tables and product images from the template data.
# Run this on the host machine where the docker containers are running.

echo "Starting Migration Data Import..."

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

if [ ! -d "${TEMPLATE_DIR}" ]; then
    echo "Error: Template data directory not found at ${TEMPLATE_DIR}"
    exit 1
fi

echo "1/2: Importing database tables into gembira-db container..."
if [ -f "${SQL_FILE}" ]; then
    cat "${SQL_FILE}" | docker exec -i gembira-db mysql -ugembira_user -pchangeme gembira_ims
    if [ $? -eq 0 ]; then
        echo "Database tables imported successfully."
    else
        echo "Error importing database tables."
    fi
else
    echo "Warning: SQL file not found at ${SQL_FILE}"
fi

echo "2/2: Restoring product images to gembira-backend container..."
if [ -d "${IMAGES_DIR}" ]; then
    # Create the directory in the container if it doesn't exist
    docker exec gembira-backend mkdir -p /app/storage/app/public/products
    # Copy images from host to container
    docker cp "${IMAGES_DIR}"/. gembira-backend:/app/storage/app/public/products/
    
    # Also copy to the host storage mapping just in case
    mkdir -p storage/app/public/products
    cp -r "${IMAGES_DIR}"/* storage/app/public/products/ 2>/dev/null
    
    echo "Product images restored successfully."
else
    echo "Warning: Images template directory not found at ${IMAGES_DIR}"
fi

echo "Import Completed!"
