#!/usr/bin/env bash

# Nnomarchy on-boot initialization script.
# Automatically detects the hardware, applies necessary runtime tweaks,
# and sets the correct screen resolution/scaling.

# 1. Automatically configure optimal screen resolution and scaling
nnomarchy-hyprland-monitor-scaling-cycle >/dev/null 2>&1

# 2. Hardware-specific runtime tweaks
if nnomarchy-hw-match "Laptop 16"; then
  # Framework 16 specific tweaks
  nnomarchy-theme-set-keyboard-f16 >/dev/null 2>&1
fi

if nnomarchy-hw-asus-rog; then
  # Asus ROG specific tweaks
  nnomarchy-theme-set-keyboard-asus-rog >/dev/null 2>&1
fi

# 3. Declarative hardware configuration check (nixos-hardware)
# This part ensures that if we are on an installed system, the correct
# nixos-hardware module is selected in the configuration.
# Skip this in the Live ISO environment
if [[ $USER == "nixos" ]] || [[ -f /etc/nixos/hosts/live-iso.nix ]]; then
    exit 0
fi

HW_FILE="/etc/nixos/hardware-selection.nix"
if [ -w "$HW_FILE" ]; then
    PRODUCT_NAME=$(cat /sys/class/dmi/id/product_name 2>/dev/null || echo "Unknown")
    BOARD_NAME=$(cat /sys/class/dmi/id/board_name 2>/dev/null || echo "Unknown")
    CPU_VENDOR=$(lscpu | grep "Vendor ID" | awk '{print $3}')
    
    NEW_HW_MODULES=""
    
    if [[ "$CPU_VENDOR" == "AuthenticAMD" ]]; then
        NEW_HW_MODULES="inputs.nixos-hardware.nixosModules.common-cpu-amd"
    elif [[ "$CPU_VENDOR" == "GenuineIntel" ]]; then
        NEW_HW_MODULES="inputs.nixos-hardware.nixosModules.common-cpu-intel"
    fi

    # Auto-detect specific known models for nixos-hardware
    if echo "$PRODUCT_NAME" | grep -qi "XPS 15 9500"; then
        NEW_HW_MODULES="$NEW_HW_MODULES\n    inputs.nixos-hardware.nixosModules.dell-xps-15-9500"
    elif echo "$PRODUCT_NAME" | grep -qi "XPS 13"; then
        NEW_HW_MODULES="$NEW_HW_MODULES\n    inputs.nixos-hardware.nixosModules.dell-xps-13-9300" # fallback example
    elif echo "$PRODUCT_NAME" | grep -qi "Framework Laptop 16"; then
        NEW_HW_MODULES="$NEW_HW_MODULES\n    inputs.nixos-hardware.nixosModules.framework-16-7040-amd"
    elif echo "$PRODUCT_NAME" | grep -qi "Framework Laptop 13"; then
        NEW_HW_MODULES="$NEW_HW_MODULES\n    inputs.nixos-hardware.nixosModules.framework-13-7040-amd"
    elif echo "$PRODUCT_NAME" | grep -qi "Surface"; then
        NEW_HW_MODULES="$NEW_HW_MODULES\n    inputs.nixos-hardware.nixosModules.microsoft-surface-pro-8" # fallback example
    elif echo "$PRODUCT_NAME" | grep -qi "Zephyrus G14"; then
        NEW_HW_MODULES="$NEW_HW_MODULES\n    inputs.nixos-hardware.nixosModules.asus-zephyrus-g14"
    elif echo "$PRODUCT_NAME" | grep -qi "ThinkPad X1 Carbon"; then
        NEW_HW_MODULES="$NEW_HW_MODULES\n    inputs.nixos-hardware.nixosModules.lenovo-thinkpad-x1-carbon-gen9"
    fi
    
    # Check if the current HW file differs from our detection
    if [ -n "$NEW_HW_MODULES" ] && ! grep -q "common-cpu" "$HW_FILE"; then
        # This is a basic detection. We overwrite it if it's completely empty or missing common-cpu.
        # It's better to let the user know, or auto-apply. We'll auto-apply for a smooth experience.
        cat <<EOF > "$HW_FILE.tmp"
{ inputs, ... }:
{
  imports = [
    $NEW_HW_MODULES
  ];
}
EOF
        if ! cmp -s "$HW_FILE" "$HW_FILE.tmp"; then
            mv "$HW_FILE.tmp" "$HW_FILE"
            # We notify the user instead of running sys-update silently, as it requires root and time.
            notify-send -u normal "Hardware Auto-Detection" "New hardware profile detected. Please run 'sys-update' when ready."
        else
            rm "$HW_FILE.tmp"
        fi
    fi
fi
