{ config, lib, inputs, pkgs, timeZone, ... }:
let
  deskCfg = config.deskCfg;
in
{
  imports = [
    inputs.nixos-hardware.nixosModules.framework-12-13th-gen-intel
    ./nixos/hardware-configuration.nix
    ../../shared/modules/desktop/base.nix
    ../../shared/modules/desktop/stylix.nix
    ../../shared/modules/desktop/silent-boot.nix
    ../../shared/modules/desktop/greetd.nix
    ../../shared/modules/wm/niri.nix
    ../../shared/modules/services/syncthing.nix
    ../../shared/modules/services/kdeconnect.nix
    ../../shared/modules/services/docker.nix
    ../../shared/modules/services/colemak-ec.nix
    ../../shared/modules/services/hyprvoice.nix
  ];

  deskCfg = {
    userName = "nate";
    hostName = "frame12";
    fullName = "Nate Anderson";
    de = "niri";
  };

  # Limit the number of generations to keep
  boot.loader.systemd-boot.configurationLimit = 5;
  boot.kernelPackages = pkgs.linuxPackages_latest;

  # Systemd initrd for Plymouth and faster boot
  boot.initrd.systemd.enable = true;

  # Silent boot
  silentBoot.enable = true;

  # Plymouth — custom Framework theme
  boot.plymouth = {
    enable = true;
    theme = "framework";
    themePackages = [
      (pkgs.runCommand "plymouth-framework-theme" { } ''
        mkdir -p $out/share/plymouth/themes/framework
        cp -r ${./framework-plymouth-theme/framework}/* $out/share/plymouth/themes/framework/
        substituteInPlace $out/share/plymouth/themes/framework/framework.plymouth \
          --replace-fail "@IMAGEDIR@" "$out/share/plymouth/themes/framework"
      '')
    ];
  };

  # Perform garbage collection weekly to maintain low disk usage
  nix.gc = {
    automatic = true;
    dates = "weekly";
    options = "--delete-older-than 14d";
  };
  nix.settings.auto-optimise-store = true;

  # IWD wireless backend
  networking.wireless.iwd.enable = true;

  # Extra groups
  main_user.extraGroups = [ "corectrl" "dialout" "docker" ];

  # Stylix overrides — kimber dark theme, terminal font size 16
  stylix = {
    base16Scheme = "${pkgs.base16-schemes}/share/themes/kimber.yaml";
    fonts.sizes.terminal = 16;
  };

  # Intel graphics acceleration (Framework 12)
  hardware.graphics.enable = true;
  hardware.enableRedistributableFirmware = true;

  # Intel iHD GPU
  environment.sessionVariables.LIBVA_DRIVER_NAME = "iHD";

  # Niri WM config
  niriwm = {
    enable = true;
    useNonFree = true;
    user = deskCfg.userName;
    installGaming = true;
    systemPackages = with pkgs; [
      libreoffice
      rpi-imager
    ];
  };

  # Frame12-specific packages
  environment.systemPackages = with pkgs; [
    cryptsetup
    cage
    man-pages
    man-pages-posix
    pavucontrol
    swayosd
    wdisplays
  ];

  # Colemak-DH EC remap
  colemakEc.enable = true;

  # Docker — rootless
  dockerConfig.rootless = true;

  # Hyprvoice dictation
  services.hyprvoice = {
    enable = true;
    user = deskCfg.userName;
  };

  # Bluetooth name
  hardware.bluetooth.settings.General.Name = "Nate-Frame";

  # udev rules — betaflight configurator + i2c for ddcutil
  services.udev.extraRules = ''
    # For betaflight configurator
    # DFU (Internal bootloader for STM32 and AT32 MCUs)
    SUBSYSTEM=="usb", ATTRS{idVendor}=="2e3c", ATTRS{idProduct}=="df11", MODE="0664", GROUP="dialout"
    SUBSYSTEM=="usb", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="df11", MODE="0664", GROUP="dialout"
    # For ddcutil monitor controls
    KERNEL=="i2c-[0-9]*", GROUP="i2c", MODE="0660"
  '';

  system.stateVersion = "25.05";
}
