#!/bin/sh

# Copyright © Michal Čihař <michal@weblate.org>
#
# SPDX-License-Identifier: GPL-3.0-or-later

# Migrations test executor

. ci/lib.sh

if [ -n "$1" ]; then
    TAG="weblate-$1"
else
    echo "Missing version to migrate from!"
    exit 1
fi

HEAD_COMMIT=$(git rev-parse HEAD)

# Copy the current file to survive checkout
MIGRATE_DIR=$(mktemp -d)
cp ci/migrate-override.txt "$MIGRATE_DIR/migrate-override.txt"
cp -r ci/migrate-scripts "$MIGRATE_DIR/"

print_step "Testing migration from $TAG on $CI_DATABASE..."
cleanup_database
check
if ! git fetch --depth 1 origin tag "$TAG"; then
    git remote add upstream https://github.com/WeblateOrg/weblate.git
    git fetch --depth 1 upstream tag "$TAG"
fi
check
git checkout "$TAG"
check
# Use clean Python environment for each version, this avoids problems when trying to downgrade from current versions
if semver_compare "$1" "<" "5.14"; then
    uv venv --python python3.11 ".venv-$TAG"
else
    uv venv --python python3.13 ".venv-$TAG"
fi
check
# shellcheck source=/dev/null
. ".venv-$TAG/bin/activate"
# Install matching Weblate package
# - Need to keep "ci" as Weblate 5.8 and older have the dependency separately
# - Force PyGobject 3.52 to be compatible with girepository-2.0
uv pip install --override "$MIGRATE_DIR/migrate-override.txt" -e ".[all,ci,mysql]"
check
echo "DATABASES['default']['HOST'] = '$CI_DB_HOST'" >> weblate/settings_test.py
check
if [ -n "$CI_DB_PASSWORD" ]; then
    echo "DATABASES['default']['PASSWORD'] = '$CI_DB_PASSWORD'" >> weblate/settings_test.py
    check
fi
if [ -n "$CI_DB_PORT" ]; then
    echo "DATABASES['default']['PORT'] = '$CI_DB_PORT'" >> weblate/settings_test.py
    check
fi
./manage.py migrate
check

# Delete automatically created languages to be able to load fixture
./manage.py shell < "$MIGRATE_DIR/migrate-scripts/setup-languages.py"
check

# Load basic project fixture from the older version
./manage.py loaddata simple-project.json
check

# Setup migration testing
./manage.py shell < "$MIGRATE_DIR/migrate-scripts/setup-base.py"
check

# Add a pending unit
semver_compare "$1" "<" "5.12"
pending_migration_test=$?
if [ "$pending_migration_test" -eq 0 ]; then
    ./manage.py shell < "$MIGRATE_DIR/migrate-scripts/setup-pending.py"
    check
fi

semver_compare "$1" ">" "5.5"
addon_scopes=$?
# Create addons with different scopes
semver_compare "$1" "<" "5.12"
file_format_params=$?

if [ "$addon_scopes" -eq 0 ] && [ "$file_format_params" -eq 0 ]; then
    ./manage.py shell < "$MIGRATE_DIR/migrate-scripts/setup-addon-customize.py"
    check
fi

# File format encoding migration
semver_compare "$1" "<" "5.16"
file_format_encoding_merge=$?
if [ "$file_format_encoding_merge" -eq 0 ]; then
    ./manage.py shell < "$MIGRATE_DIR/migrate-scripts/setup-formats.py"
    check
fi

# Add users with billing permission
semver_compare "$1" "<" "5.16"
billing_users_migrate=$?
if [ "$billing_users_migrate" -eq 0 ]; then
    ./manage.py shell < "$MIGRATE_DIR/migrate-scripts/setup-billing.py"
    check
fi

git reset --hard
check
git checkout "$HEAD_COMMIT"
check
# Use CI environment
deactivate
check
run_coverage ./manage.py migrate
check
# Check migrated vote exists
uv run --all-extras ./manage.py shell < "$MIGRATE_DIR/migrate-scripts/assert-vote.py"
check

# Check migrated pending unit
if [ "$pending_migration_test" -eq 0 ]; then
    uv run --all-extras ./manage.py shell < "$MIGRATE_DIR/migrate-scripts/assert-pending.py"
fi

if [ "$addon_scopes" -eq 0 ] && [ "$file_format_params" -eq 0 ]; then
    # check that relevant addon parameters have been migrated to file_format_params
    uv run --all-extras ./manage.py shell < "$MIGRATE_DIR/migrate-scripts/assert-params.py"
    check
fi

if [ "$file_format_encoding_merge" -eq 0 ]; then
    # check that different encoding file-formats are merged into a single one with correct file format params
    uv run --all-extras ./manage.py shell < "$MIGRATE_DIR/migrate-scripts/assert-encoding.py"
fi

if [ "$billing_users_migrate" -eq 0 ]; then
    # Verify that the user has been migrated from the billing permission to the owner
    uv run --all-extras ./manage.py shell < "$MIGRATE_DIR/migrate-scripts/assert-billing.py"
fi
