{ pkgs, config, lib, ... }:
with lib;
let
  cfg = config.eboskma.programs.tea;

  yamlFormat = pkgs.formats.yaml { };

  loginsType = types.submodule {
    options = {
      name = mkOption {
        type = types.str;
        description = "Name of the login";
      };
      url = mkOption {
        type = types.str;
        description = "URL to the gitea server";
      };
      token = mkOption {
        type = types.str;
        description = "Gitea API access token";
      };
      default = mkOption {
        type = types.bool;
        description = "Whether this is the default login";
      };
      ssh_host = mkOption {
        type = types.str;
        description = "Hostname for the SSH server";
      };
      ssh_key = mkOption {
        type = types.str;
        default = "";
        description = "Optional path to the SSH private key";
      };
      insecure = mkOption {
        type = types.bool;
        default = false;
        description = "Whether to skip TLS verification";
      };
      user = mkOption {
        type = types.str;
        description = "Gitea username";
      };
    };
  };

  flagDefaultsType = types.submodule {
    options = {
      remote = mkOption {
        type = types.nullOr types.str;
        description = "Prefer a specific git remote to use to select a repository on gitea, instead of relying on the remote associated with main/master/trunk branch. The --remote flag still has precedence over this value.";
        default = null;
      };
    };
  };

  preferencesType = types.submodule {
    options = {
      editor = mkOption {
        type = types.bool;
        description = "Prefer using an external text editor over inline multiline prompts";
        default = false;
      };
      flag_defaults = mkOption {
        type = types.nullOr flagDefaultsType;
        default = null;
      };
    };
  };

  settingsType = types.submodule {
    freeformType = yamlFormat.type;

    options = {
      logins = mkOption {
        type = types.listOf loginsType;
        default = [ ];
        description = "List of gitea logins";
      };
      preferences = mkOption {
        type = types.nullOr preferencesType;
        default = null;
      };
    };
  };

in
{
  options.eboskma.programs.tea = {
    enable = mkEnableOption "tea";

    settings = mkOption {
      type = settingsType;
      default = { };
      description = "Configuration written to <filename>$XDG_CONFIG_HOME/tea/config.yml</filename>.";
    };
  };

  config = mkIf cfg.enable {
    home.packages = [ pkgs.tea ];

    # xdg.configFile."tea/config.yml".source = yamlFormat.generate "tea-config.yml" cfg.settings;
  };
}