#!/usr/bin/env bash

set -euo pipefail

script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
project_root="$(cd -- "${script_dir}/.." && pwd)"
backend="${project_root}/backend"

cd "${backend}"

if [[ ! -f phpunit.xml ]] || ! grep -q '<env name="DB_DATABASE" value="plocal_local_test"' phpunit.xml || ! grep -q "env('DB_HOST', '127.0.0.1')" config/database.php; then
    echo "Refusing acceptance: PHPUnit must target plocal_local_test and the MySQL default host must be loopback." >&2
    exit 2
fi

if grep -qE '<env name="DB_DATABASE" value="(plocal_local|plocal_a4_cleanroom_20260818|plocal_a4_restore_20260818)"' phpunit.xml; then
    echo "Refusing acceptance: protected or drill-only database configured as the PHPUnit target." >&2
    exit 2
fi

composer validate --strict
./vendor/bin/pint --test

filters=(
    Phase4BProfessionalCategoryRatingEngineTest
    Phase4CInternalEndToEndProfessionalEvaluationTest
    Phase4DPrivateAuthenticationFoundationTest
    Phase4EADemoReadyEvaluatorWorkspaceTest
    Phase4EA1BrowserSetupAndUserManagementTest
    Phase4EA3BObjectiveHardeningTest
)

for filter in "${filters[@]}"; do
    php artisan test --filter="${filter}"
done

php artisan test

echo "PLOCAL sequential local acceptance passed. Do not parallelize these MySQL fixture suites."
