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

let
  cfg = config.services.iam;
  tomlFormat = pkgs.formats.toml { };
  generatedConfig = {
    server = {
      addr = "0.0.0.0:${toString cfg.port}";
      http_addr = "0.0.0.0:${toString cfg.httpPort}";
    };
    logging.level = "info";
    admin = {
      allow_unauthenticated = cfg.allowUnauthenticatedAdmin;
    };
    dev = {
      allow_random_signing_key = cfg.allowRandomSigningKey;
      allow_memory_backend = cfg.storeBackend == "memory";
    };
    store = {
      backend = cfg.storeBackend;
      flaredb_endpoint =
        if cfg.flaredbAddr != null
        then cfg.flaredbAddr
        else "127.0.0.1:2479";
      flaredb_namespace = "iam";
      single_node = cfg.singleNode;
    }
    // lib.optionalAttrs (cfg.databaseUrl != null) {
      database_url = cfg.databaseUrl;
    };
    cluster = lib.optionalAttrs (cfg.chainfireAddr != null) {
      chainfire_endpoint = cfg.chainfireAddr;
    };
  };
  iamConfigFile = tomlFormat.generate "iam.toml" (lib.recursiveUpdate generatedConfig cfg.settings);
in
{
  options.services.iam = {
    enable = lib.mkEnableOption "iam service";

    port = lib.mkOption {
      type = lib.types.port;
      default = 50080;
      description = "Port for iam gRPC API";
    };

    httpPort = lib.mkOption {
      type = lib.types.port;
      default = 8083;
      description = "Port for IAM HTTP REST API";
    };

    chainfireAddr = lib.mkOption {
      type = lib.types.nullOr lib.types.str;
      default = null;
      description = "ChainFire endpoint address (host:port) for cluster coordination";
      example = "10.0.0.1:2379";
    };

    flaredbAddr = lib.mkOption {
      type = lib.types.nullOr lib.types.str;
      default = null;
      description = "FlareDB endpoint address (host:port) for metadata/user data";
      example = "10.0.0.1:2479";
    };

    storeBackend = lib.mkOption {
      type = lib.types.enum [ "flaredb" "postgres" "sqlite" "memory" ];
      default = "flaredb";
      description = "IAM store backend. Use memory only for test/dev.";
    };

    databaseUrl = lib.mkOption {
      type = lib.types.nullOr lib.types.str;
      default = null;
      description = "SQL database URL for IAM store (required when storeBackend is postgres/sqlite).";
      example = "postgres://iam:secret@10.0.0.10:5432/iam";
    };

    singleNode = lib.mkOption {
      type = lib.types.bool;
      default = false;
      description = "Enable single-node mode (required when store backend is SQLite)";
    };

    dataDir = lib.mkOption {
      type = lib.types.path;
      default = "/var/lib/iam";
      description = "Data directory for iam";
    };

    adminToken = lib.mkOption {
      type = lib.types.nullOr lib.types.str;
      default = null;
      description = "Admin token injected as IAM_ADMIN_TOKEN for privileged IAM APIs.";
    };

    allowRandomSigningKey = lib.mkOption {
      type = lib.types.bool;
      default = false;
      description = "Allow IAM to generate a random signing key when authn.internal_token.signing_key is unset (dev only).";
    };

    allowUnauthenticatedAdmin = lib.mkOption {
      type = lib.types.bool;
      default = false;
      description = "Allow privileged admin APIs without an admin token (dev only).";
    };

    settings = lib.mkOption {
      type = lib.types.attrs;
      default = {};
      description = "Additional configuration settings";
    };

    package = lib.mkOption {
      type = lib.types.package;
      default = pkgs.iam-server or (throw "iam-server package not found");
      description = "Package to use for iam";
    };
  };

  config = lib.mkIf cfg.enable {
    # Create system user
    users.users.iam = {
      isSystemUser = true;
      group = "iam";
      description = "IAM service user";
      home = cfg.dataDir;
    };

    users.groups.iam = {};

    # Create systemd service
    systemd.services.iam = {
      description = "IAM Identity and Access Management Service";
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" "chainfire.service" "flaredb.service" ];
      wants = [ "chainfire.service" "flaredb.service" ];

      environment = lib.mkMerge [
        (lib.mkIf (cfg.adminToken != null) {
          IAM_ADMIN_TOKEN = cfg.adminToken;
        })
      ];

      serviceConfig = {
        Type = "simple";
        User = "iam";
        Group = "iam";
        Restart = "on-failure";
        RestartSec = "10s";

        # State directory management
        StateDirectory = "iam";
        StateDirectoryMode = "0750";

        # Security hardening
        NoNewPrivileges = true;
        PrivateTmp = true;
        ProtectSystem = "strict";
        ProtectHome = true;
        ReadWritePaths = [ cfg.dataDir ];

        # Start command
        ExecStart = "${cfg.package}/bin/iam-server --config ${iamConfigFile}";
      };
    };
  };
}
