#!/bin/bash
set -e

echo "=== Testing fresh migration ==="
php artisan migrate:fresh --force --seed 2>&1

echo ""
echo "=== Verifying table count ==="
php artisan tinker --execute="echo 'Tables: ' . count(\DB::select(\"SELECT tablename FROM pg_tables WHERE schemaname='public'\"));"

echo ""
echo "=== Verifying key model counts ==="
php artisan tinker --execute="
echo 'Organizations: ' . \App\Domain\Shared\Models\Academy::count() . PHP_EOL;
echo 'Users: ' . \App\Models\User::count() . PHP_EOL;
echo 'Permissions: ' . \App\Domain\Identity\Models\Permission::count() . PHP_EOL;
echo 'Roles: ' . \App\Domain\Identity\Models\Role::count() . PHP_EOL;
"

echo ""
echo "=== Running route:list to verify no broken controllers ==="
php artisan route:list --compact 2>&1 | tail -5

echo ""
echo "=== Testing login flow ==="
php artisan tinker --execute="
\$user = \App\Models\User::where('email', 'admin@elcaptain.com')->first();
echo \$user ? 'Login user exists: YES' : 'Login user exists: NO';
"

echo ""
echo "=== All checks passed ==="
