#!/bin/bash

# Function to run migrations for a specific folder
run_migrations_in_folder() {
    folder_path="database/migrations/Api/$1"
    php artisan migrate --path="$folder_path"
    if [ $? -eq 0 ]; then
        echo "Migrations in $folder_path successful."
    else
        echo "Error: Migration failed in $folder_path. Script aborted."
        exit 1
    fi
}

# Run migrations in order (modify the order as needed)
run_migrations_in_folder V1
run_migrations_in_folder V2
run_migrations_in_folder V3

# Run migrations without folder (if any exist)
php artisan migrate
# Seed the database only if all migrations succeed
if [ $? -eq 0 ]; then
php artisan db:seed
echo "Seeding successful."
else
echo "Seeding skipped due to migration errors."
fi

# Instal passport after all migrations and all seeders have been successfully runed
if [ $? -eq 0 ]; then
php artisan passport:install
echo "Passport successfully installed. Copy the keys and past the .ENV file. 'PASSPORT_PERSONAL_ACCESS_CLIENT_ID', 'PASSPORT_PERSONAL_ACCESS_CLIENT_SECRET' add two keys."
else
echo "Passport install failed."
fi

