#!/bin/bash
# ==============================================================================
# karch module: base
# Phase 1 — Install base system via pacstrap and generate fstab
# ==============================================================================

run_base() {
    msg_info "Preparing base system installation..."

    # --- Build package list --------------------------------------------------
    local packages=(
        base
        linux
        linux-firmware
        sudo
        networkmanager
        wpa_supplicant
        dialog
        nano
        vi
        vim
        htop
        fastfetch
    )

    # Auto-detect CPU microcode
    local ucode
    ucode="$(detect_cpu_vendor)"
    if [[ -n "$ucode" ]]; then
        msg_info "Detected CPU: installing $ucode"
        packages+=("$ucode")
        write_env "UCODE_PKG" "$ucode"
    else
        msg_warn "Could not detect CPU vendor — skipping microcode package"
        write_env "UCODE_PKG" ""
    fi

    # Auto-detect Intel Wi-Fi
    if detect_intel_wifi; then
        msg_info "Detected Intel Wi-Fi adapter: installing linux-firmware-intel"
        packages+=("linux-firmware-intel")
        write_env "HAS_INTEL_WIFI" "1"
    else
        write_env "HAS_INTEL_WIFI" "0"
    fi

    # Auto-detect GPU(s) — saved for Phase 2 driver installation
    local gpu_vendors
    gpu_vendors="$(detect_gpu)"
    if [[ -n "$gpu_vendors" ]]; then
        msg_info "Detected GPU vendor(s): $gpu_vendors"
        write_env "GPU_VENDORS" "$gpu_vendors"

        # Detect NVIDIA generation for driver selection
        if [[ " $gpu_vendors " == *" nvidia "* ]]; then
            local nvidia_gen
            nvidia_gen="$(detect_nvidia_generation)"
            msg_info "NVIDIA generation: $nvidia_gen"
            write_env "NVIDIA_GEN" "$nvidia_gen"
        fi
    else
        msg_warn "Could not detect GPU — skipping graphics driver auto-detection"
        write_env "GPU_VENDORS" ""
    fi

    # --- Show package list and confirm ---------------------------------------
    local pkg_display
    pkg_display=$(printf '%s\n' "${packages[@]}" | sort)

    dialog_msgbox "Base Installation" \
        "The following packages will be installed:\n\n${pkg_display}\n\nThis may take a while depending on your connection."

    # --- Sync clock ----------------------------------------------------------
    msg_info "Synchronising system clock..."
    timedatectl set-ntp true 2>/dev/null || true

    # --- Pacstrap ------------------------------------------------------------
    msg_info "Installing base system (pacstrap)..."
    if ! pacstrap -K /mnt "${packages[@]}"; then
        msg_error "pacstrap failed!"
        return 1
    fi

    # --- Generate fstab ------------------------------------------------------
    msg_info "Generating fstab..."
    genfstab -U /mnt > /mnt/etc/fstab

    msg_ok "Base system installed and fstab generated."

    # Show fstab for review
    dialog_msgbox "fstab" "Generated /etc/fstab:\n\n$(cat /mnt/etc/fstab)"

    return 0
}
