{ config, pkgs, inputs, lib, ... }:

# Minimal TTY Installer ISO Configuration
#
# This creates a minimal, text-only installation environment.
# No desktop environment - just TTY with gum-based installer.

{
  imports = [
    ../core/system/nix.nix
  ];

  # Base installation media configuration is handled by the module imported in flake.nix

  # Console configuration for a pleasant TTY experience
  console = {
    font = "ter-v16n";
    packages = [ pkgs.terminus_font ];
  };

  # Essential packages for installation
  environment.systemPackages = with pkgs; [
    # Core utilities
    git
    vim
    curl
    wget

    # TUI installer dependencies
    gum

    # Disk tools
    inputs.disko.packages.${pkgs.stdenv.hostPlatform.system}.disko
    parted
    btrfs-progs
    cryptsetup

    # Network tools
    networkmanager

    # System info
    lshw
    pciutils
    usbutils

    # Installer command
    (pkgs.writeShellScriptBin "nomarchy-install" ''
      exec /etc/install.sh "$@"
    '')
  ];

  # Enable NetworkManager for easy network setup
  networking.networkmanager.enable = true;

  # Auto-login to TTY as root for installation
  services.getty.autologinUser = lib.mkForce "root";

  # Display welcome message and installer info
  environment.etc."motd".text = ''

    ╔══════════════════════════════════════════════════════════════╗
    ║                                                              ║
    ║                   NOMARCHY INSTALLER                         ║
    ║                                                              ║
    ║   Run 'nomarchy-install' to start the installation wizard    ║
    ║                                                              ║
    ║   For network setup: nmtui                                   ║
    ║   For manual install: see /etc/nomarchy/                     ║
    ║                                                              ║
    ╚══════════════════════════════════════════════════════════════╝

  '';

  # Make the installer script available
  environment.etc."install.sh" = {
    source = ../installer/install.sh;
    mode = "0755";
  };

  # Symlink for easy access (merged into systemPackages above)
  # The nomarchy-install script is created by writeShellScriptBin in the main list

  # Include disko configurations
  environment.etc."disko-golden.nix".source = ../installer/disko-golden.nix;

  # Include Nomarchy source for installation
  environment.etc."nomarchy".source = inputs.self;

  # Disable graphical stuff - this is TTY only
  services.xserver.enable = false;
  services.displayManager.sddm.enable = lib.mkForce false;

  # Ensure we have a proper shell environment
  programs.bash.completion.enable = true;

  # Auto-launch installer on the main TTY (tty1)
  programs.bash.loginShellInit = ''
    if [ "$(tty)" = "/dev/tty1" ]; then
      nomarchy-install
    fi
  '';

  # Include documentation
  documentation.enable = true;
  documentation.man.enable = true;
}
