#!/usr/bin/env bash

set -u

OUT="logistics_host_report_$(date +%Y%m%d_%H%M%S).txt"

section() {
    printf '\n\n===== %s =====\n' "$1" | tee -a "$OUT"
}

run() {
    printf '\n$ %s\n' "$*" | tee -a "$OUT"
    "$@" 2>&1 | tee -a "$OUT" || true
}

printf 'Logistics Platform Hosting Diagnostic\n' > "$OUT"
printf 'Generated: %s\n' "$(date -Is 2>/dev/null || date)" >> "$OUT"
printf 'Project domain: logistic.webrhinoc.com\n' >> "$OUT"
printf 'Expected project path: /home/webrhin4/logistic.webrhinoc.com\n' >> "$OUT"
printf 'This report does not print passwords, .env contents or database credentials.\n' >> "$OUT"

section "SYSTEM"
run uname -a
[ -f /etc/os-release ] && run cat /etc/os-release
run id
run pwd
run hostname
run date

section "PROJECT DIRECTORY"
run ls -la
run find . -maxdepth 3 -type f \( \
    -name artisan \
    -o -name composer.json \
    -o -name package.json \
    -o -name .htaccess \
    -o -name index.php \
\) -print

section "DIRECTORY PERMISSIONS"
run stat .
run df -h .
run df -i .
run umask

TEST_FILE="./.logistics_write_test_$$"

if touch "$TEST_FILE" 2>/dev/null; then
    echo "PROJECT_DIRECTORY_WRITABLE=yes" | tee -a "$OUT"
    rm -f "$TEST_FILE"
else
    echo "PROJECT_DIRECTORY_WRITABLE=no" | tee -a "$OUT"
fi

section "PHP"

if command -v php >/dev/null 2>&1; then
    run command -v php
    run php -v
    run php --ini

    run php -r '
        echo "PHP_VERSION=".PHP_VERSION.PHP_EOL;
        echo "PHP_SAPI=".PHP_SAPI.PHP_EOL;
        echo "memory_limit=".ini_get("memory_limit").PHP_EOL;
        echo "max_execution_time=".ini_get("max_execution_time").PHP_EOL;
        echo "upload_max_filesize=".ini_get("upload_max_filesize").PHP_EOL;
        echo "post_max_size=".ini_get("post_max_size").PHP_EOL;
        echo "max_input_vars=".ini_get("max_input_vars").PHP_EOL;
        echo "allow_url_fopen=".ini_get("allow_url_fopen").PHP_EOL;
        echo "disable_functions=".ini_get("disable_functions").PHP_EOL;
    '

    run php -m
else
    echo "PHP CLI not found" | tee -a "$OUT"
fi

section "PHP EXTENSION CHECK"

if command -v php >/dev/null 2>&1; then
    for extension in \
        bcmath \
        ctype \
        curl \
        dom \
        fileinfo \
        filter \
        hash \
        intl \
        json \
        mbstring \
        openssl \
        pcre \
        pdo \
        pdo_mysql \
        session \
        tokenizer \
        xml \
        zip
    do
        if php -m | grep -qi "^${extension}$"; then
            echo "${extension}=yes" | tee -a "$OUT"
        else
            echo "${extension}=no" | tee -a "$OUT"
        fi
    done
fi

section "COMPOSER"

if command -v composer >/dev/null 2>&1; then
    run command -v composer
    run composer --version
    run composer diagnose
elif [ -f "$HOME/composer.phar" ]; then
    run php "$HOME/composer.phar" --version
else
    echo "Composer not found in PATH" | tee -a "$OUT"
fi

section "DATABASE CLIENT"

if command -v mysql >/dev/null 2>&1; then
    run command -v mysql
    run mysql --version
else
    echo "MySQL command-line client not found" | tee -a "$OUT"
fi

if command -v mariadb >/dev/null 2>&1; then
    run mariadb --version
fi

section "NODE AND NPM"

if command -v node >/dev/null 2>&1; then
    run command -v node
    run node --version
else
    echo "Node.js not found" | tee -a "$OUT"
fi

if command -v npm >/dev/null 2>&1; then
    run command -v npm
    run npm --version
else
    echo "npm not found" | tee -a "$OUT"
fi

section "ARCHIVE TOOLS"

command -v unzip >/dev/null 2>&1 && run unzip -v || echo "unzip not found" | tee -a "$OUT"
command -v zip >/dev/null 2>&1 && run zip -v || echo "zip not found" | tee -a "$OUT"
command -v tar >/dev/null 2>&1 && run tar --version || echo "tar not found" | tee -a "$OUT"

section "CRON"

if command -v crontab >/dev/null 2>&1; then
    run command -v crontab
    run crontab -l
else
    echo "crontab not found" | tee -a "$OUT"
fi

section "OPTIONAL SERVER TOOLS"

command -v git >/dev/null 2>&1 && run git --version || echo "git not found" | tee -a "$OUT"
command -v curl >/dev/null 2>&1 && run curl --version || echo "curl not found" | tee -a "$OUT"
command -v openssl >/dev/null 2>&1 && run openssl version || echo "openssl not found" | tee -a "$OUT"

section "DOMAIN TEST"

if command -v curl >/dev/null 2>&1; then
    run curl -I -L --max-time 20 https://logistic.webrhinoc.com
fi

section "HTACCESS SUPPORT FILES"

for file in \
    .htaccess \
    public/.htaccess \
    index.php \
    public/index.php \
    artisan \
    composer.json
do
    if [ -e "$file" ]; then
        ls -la "$file" 2>&1 | tee -a "$OUT"
    else
        echo "${file}=not_present" | tee -a "$OUT"
    fi
done

section "EXISTING PROJECT DETECTION"

if [ -f artisan ] && [ -f composer.json ]; then
    run php artisan --version

    php -r '
        $data = json_decode(file_get_contents("composer.json"), true);
        echo "PROJECT_NAME=".($data["name"] ?? "unknown").PHP_EOL;
        echo "LARAVEL_REQUIREMENT=".($data["require"]["laravel/framework"] ?? "unknown").PHP_EOL;
    ' 2>&1 | tee -a "$OUT" || true
else
    echo "No existing Laravel project detected in this directory" | tee -a "$OUT"
fi

section "RESULT"

echo "Domain: logistic.webrhinoc.com" | tee -a "$OUT"
echo "Project path: /home/webrhin4/logistic.webrhinoc.com" | tee -a "$OUT"
echo "Report file: $OUT" | tee -a "$OUT"

printf '\nDiagnostic completed successfully.\n'
printf 'Report saved at:\n%s/%s\n' "$(pwd)" "$OUT"
