# ============================================================================
# Fleet Option Definition
# ============================================================================
# This module only defines the athenix.fleet option without any dependencies.
# Used by fleet/default.nix to evaluate inventory data without circular dependencies.
{ inputs, lib, ... }:
let
  fleetDefinition = lib.mkOption {
    description = "Hardware types definitions for the fleet.";
    type = lib.types.attrsOf (
      lib.types.submodule (
        { name, ... }:
        {
          options = {
            type = lib.mkOption {
              type = lib.types.oneOf [
                lib.types.str
                lib.types.listOf
                lib.types.str
              ];
              default = name;
              description = "Type(s) of system configuration for this device.";
            };
            system = lib.mkOption {
              type = lib.types.str;
              default = "x86_64-linux";
              description = "NixOS system architecture for this hardware type.";
            };
            devices = lib.mkOption {
              type = lib.types.oneOf [
                lib.types.int
                (lib.types.attrsOf (
                  lib.types.submodule (
                    { name, ... }:
                    {
                      freeformType = lib.types.attrs;
                    }
                  )
                ))
              ];
            };
            count = lib.mkOption {
              type = lib.types.int;
              default = 0;
              description = "Number of devices of this type to create.";
            };
            defaultCount = lib.mkOption {
              type = lib.types.int;
              default = 0;
              description = "Default number of devices to create with default configurations and numbered hostnames.";
            };
            overrides = lib.mkOption {
              type = lib.types.attrs;
              default = { };
              description = "Overrides to apply to all devices of this type.";
            };
          };
        }
      )
    );
  };
  # Submodule defining the structure of a user account
  userSubmodule = lib.types.submodule {
    options = {
      isNormalUser = lib.mkOption {
        type = lib.types.bool;
        default = true;
      };
      description = lib.mkOption {
        type = lib.types.nullOr lib.types.str;
        default = null;
      };
      extraGroups = lib.mkOption {
        type = lib.types.listOf lib.types.str;
        default = [ ];
      };
      hashedPassword = lib.mkOption {
        type = lib.types.str;
        default = "!";
      };
      extraPackages = lib.mkOption {
        type = lib.types.listOf lib.types.package;
        default = [ ];
      };
      excludePackages = lib.mkOption {
        type = lib.types.listOf lib.types.package;
        default = [ ];
      };
      homePackages = lib.mkOption {
        type = lib.types.listOf lib.types.package;
        default = [ ];
      };
      extraImports = lib.mkOption {
        type = lib.types.listOf lib.types.path;
        default = [ ];
      };
      external = lib.mkOption {
        type = lib.types.nullOr (
          lib.types.oneOf [
            lib.types.path
            lib.types.package
            lib.types.attrs
          ]
        );
        default = null;
        description = ''
          External user configuration module. Can be:
          - A path to a local module directory
          - A fetchGit/fetchTarball result pointing to a repository

          The external module can contain:
          - user.nix (optional): Sets athenix.users.<name> options AND home-manager config
          - nixos.nix (optional): System-level NixOS configuration

          Example: builtins.fetchGit { url = "https://github.com/user/dotfiles"; rev = "..."; }
        '';
      };
      opensshKeys = lib.mkOption {
        type = lib.types.listOf lib.types.str;
        default = [ ];
        description = "List of SSH public keys for the user.";
      };
      shell = lib.mkOption {
        type = lib.types.nullOr (
          lib.types.enum [
            "bash"
            "zsh"
            "fish"
            "tcsh"
          ]
        );
        default = "bash";
        description = "The shell for this user.";
      };
      editor = lib.mkOption {
        type = lib.types.nullOr (
          lib.types.enum [
            "vim"
            "neovim"
            "emacs"
            "nano"
            "code"
          ]
        );
        default = "neovim";
        description = "The default editor for this user.";
      };
      useZshTheme = lib.mkOption {
        type = lib.types.bool;
        default = true;
        description = "Whether to apply the system Zsh theme.";
      };
      useNvimPlugins = lib.mkOption {
        type = lib.types.bool;
        default = true;
        description = "Whether to apply the system Neovim configuration.";
      };
      enable = lib.mkOption {
        type = lib.types.bool;
        default = false;
        description = "Whether this user account is enabled on this system.";
      };
    };
  };
in
{
  options.athenix = {
    fleet = fleetDefinition;
    hwTypes = lib.mkOption {
      description = "Hardware types definitions for the fleet.";
      type = lib.types.attrs;
    };
    users = lib.mkOption {
      type = lib.types.attrsOf userSubmodule;
      default = { };
      description = "User accounts configuration. Set enable=true for users that should exist on this system.";
    };
  };

  config.athenix.hwTypes = lib.mkDefault (import ../hw { inherit inputs; });
}
