#!/bin/sh
set -e

APP_DIR="/var/www/html"

log() {
    echo "$1"
}

normalize_railway_database_env() {
    if [ -z "${DB_CONNECTION:-}" ]; then
        export DB_CONNECTION="mysql"
    fi

    if [ "${DB_CONNECTION:-}" = "mysql" ] && [ -n "${MYSQLHOST:-}" ]; then
        export DB_HOST="${DB_HOST:-$MYSQLHOST}"
        export DB_PORT="${DB_PORT:-${MYSQLPORT:-3306}}"
        export DB_DATABASE="${DB_DATABASE:-${MYSQLDATABASE:-}}"
        export DB_USERNAME="${DB_USERNAME:-${MYSQLUSER:-}}"
        export DB_PASSWORD="${DB_PASSWORD:-${MYSQLPASSWORD:-}}"
    fi
}

wait_for_database() {
    max_attempts="${DB_WAIT_MAX_ATTEMPTS:-30}"
    sleep_seconds="${DB_WAIT_SLEEP_SECONDS:-2}"
    attempt=1

    while [ "$attempt" -le "$max_attempts" ]; do
        if php -r '
            $host = getenv("DB_HOST") ?: "127.0.0.1";
            $port = getenv("DB_PORT") ?: "3306";
            $db = getenv("DB_DATABASE") ?: "";
            $user = getenv("DB_USERNAME") ?: "";
            $pass = getenv("DB_PASSWORD") ?: "";
            $dsn = sprintf("mysql:host=%s;port=%s;dbname=%s;charset=utf8mb4", $host, $port, $db);

            new PDO($dsn, $user, $pass, [PDO::ATTR_TIMEOUT => 5]);
        ' >/dev/null 2>&1; then
            return 0
        fi

        log "Database not reachable yet (attempt ${attempt}/${max_attempts}). Retrying in ${sleep_seconds}s..."
        sleep "$sleep_seconds"
        attempt=$((attempt + 1))
    done

    log "ERROR: Database connection failed after ${max_attempts} attempts."
    return 1
}

run_startup_migrations() {
    if [ "${RUN_MIGRATIONS_ON_START:-false}" != "true" ]; then
        return 0
    fi

    log "RUN_MIGRATIONS_ON_START=true, waiting for database..."
    wait_for_database
    log "Running startup migrations..."
    php artisan migrate --force --no-interaction
}

prepare_laravel_runtime() {
    if [ -z "${APP_KEY:-}" ]; then
        log "ERROR: APP_KEY environment variable is not set."
        exit 1
    fi

    if [ ! -L "public/storage" ]; then
        php artisan storage:link --force >/dev/null 2>&1 || true
    fi

    php artisan config:cache
    php artisan route:cache >/dev/null 2>&1 || log "Route cache skipped (closures found or route cache not compatible)."
    php artisan view:cache >/dev/null 2>&1 || log "View cache skipped."
}

log "Starting Laravel application..."
cd "$APP_DIR"

normalize_railway_database_env
run_startup_migrations
prepare_laravel_runtime

log "Laravel is ready. Starting server on port ${PORT:-8080}..."
exec php artisan serve --host=0.0.0.0 --port="${PORT:-8080}"
