# frontend_doctor
# ----------------
# Diagnostica la salud de un proyecto frontend Mantine.
# Verifica dependencias, configuracion y versiones.
# Imprime tabla de checks y retorna exit code 0 (ok) o 1 (fallos).
#
# USO (sourced):
#   source frontend_doctor.sh
#   frontend_doctor /path/to/frontend
#
# USO (directo):
#   bash frontend_doctor.sh /path/to/frontend

frontend_doctor() {
    local project_dir="$1"
    local failures=0

    if [ -z "$project_dir" ]; then
        echo "frontend_doctor: se requiere project_dir" >&2
        return 1
    fi

    if [ ! -f "$project_dir/package.json" ]; then
        echo "frontend_doctor: no existe package.json en $project_dir" >&2
        return 1
    fi

    echo "=== Frontend Doctor: $project_dir ==="
    echo ""

    # Helper: check y reportar
    _check() {
        local label="$1"
        local ok="$2"
        local detail="$3"
        if [ "$ok" = "1" ]; then
            printf "  ✓  %-35s %s\n" "$label" "$detail"
        else
            printf "  ✗  %-35s %s\n" "$label" "$detail"
            ((failures++))
        fi
    }

    # 1. Node >= 18
    local node_ver=""
    local node_ok=0
    if command -v node &>/dev/null; then
        node_ver=$(node -v 2>/dev/null | sed 's/v//')
        local node_major=$(echo "$node_ver" | cut -d. -f1)
        [ "$node_major" -ge 18 ] 2>/dev/null && node_ok=1
    fi
    _check "Node >= 18" "$node_ok" "${node_ver:-not found}"

    # 2. Package manager
    local pm_ok=0
    local pm_name="none"
    if [ -f "$project_dir/pnpm-lock.yaml" ]; then
        pm_name="pnpm"; pm_ok=1
    elif [ -f "$project_dir/yarn.lock" ]; then
        pm_name="yarn"; pm_ok=1
    elif [ -f "$project_dir/package-lock.json" ]; then
        pm_name="npm"; pm_ok=1
    fi
    _check "Package manager detected" "$pm_ok" "$pm_name"

    # 3. node_modules existe
    local nm_ok=0
    [ -d "$project_dir/node_modules" ] && nm_ok=1
    _check "node_modules present" "$nm_ok" ""

    # 4. @mantine/core instalado
    local mantine_ok=0
    local mantine_ver=""
    if [ -f "$project_dir/node_modules/@mantine/core/package.json" ]; then
        mantine_ver=$(node -e "console.log(require('$project_dir/node_modules/@mantine/core/package.json').version)" 2>/dev/null)
        mantine_ok=1
    fi
    _check "@mantine/core" "$mantine_ok" "${mantine_ver:-not installed}"

    # 5. @mantine/hooks
    local hooks_ok=0
    [ -d "$project_dir/node_modules/@mantine/hooks" ] && hooks_ok=1
    _check "@mantine/hooks" "$hooks_ok" ""

    # 6. @mantine/charts
    local charts_ok=0
    [ -d "$project_dir/node_modules/@mantine/charts" ] && charts_ok=1
    _check "@mantine/charts" "$charts_ok" ""

    # 7. React >= 18
    local react_ok=0
    local react_ver=""
    if [ -f "$project_dir/node_modules/react/package.json" ]; then
        react_ver=$(node -e "console.log(require('$project_dir/node_modules/react/package.json').version)" 2>/dev/null)
        local react_major=$(echo "$react_ver" | cut -d. -f1)
        [ "$react_major" -ge 18 ] 2>/dev/null && react_ok=1
    fi
    _check "React >= 18" "$react_ok" "${react_ver:-not found}"

    # 8. postcss.config presente
    local postcss_ok=0
    if [ -f "$project_dir/postcss.config.cjs" ] || [ -f "$project_dir/postcss.config.js" ] || [ -f "$project_dir/postcss.config.mjs" ]; then
        postcss_ok=1
    fi
    _check "postcss.config present" "$postcss_ok" ""

    # 9. TypeScript >= 5
    local ts_ok=0
    local ts_ver=""
    if [ -f "$project_dir/node_modules/typescript/package.json" ]; then
        ts_ver=$(node -e "console.log(require('$project_dir/node_modules/typescript/package.json').version)" 2>/dev/null)
        local ts_major=$(echo "$ts_ver" | cut -d. -f1)
        [ "$ts_major" -ge 5 ] 2>/dev/null && ts_ok=1
    fi
    _check "TypeScript >= 5" "$ts_ok" "${ts_ver:-not found}"

    # 10. vite.config presente
    local vite_ok=0
    if [ -f "$project_dir/vite.config.ts" ] || [ -f "$project_dir/vite.config.js" ]; then
        vite_ok=1
    fi
    _check "vite.config present" "$vite_ok" ""

    # 11. Shadcn residual (warning)
    local shadcn_clean=1
    if [ -f "$project_dir/components.json" ]; then
        shadcn_clean=0
    fi
    _check "No shadcn residual" "$shadcn_clean" "$([ "$shadcn_clean" = "0" ] && echo 'components.json found')"

    # 12. @base-ui residual (warning)
    local baseui_clean=1
    if [ -d "$project_dir/node_modules/@base-ui" ]; then
        baseui_clean=0
    fi
    _check "No @base-ui residual" "$baseui_clean" "$([ "$baseui_clean" = "0" ] && echo '@base-ui still installed')"

    echo ""
    if [ "$failures" -eq 0 ]; then
        echo "  Resultado: todo OK"
        return 0
    else
        echo "  Resultado: $failures problema(s) encontrado(s)"
        return 1
    fi
}

if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
    frontend_doctor "$@"
fi
