{ flake, ... }:
let
  inherit (flake.config.machines.devices) mars;
  inherit (flake.config.services) instances;

  makeBookmarks =
    {
      suffix,
      urlTemplate,
      services,
    }:
    builtins.map (name: {
      name = "${instances.${name}.label} ${suffix}";
      url = urlTemplate name;
      tags = instances.${name}.tags;
      keyword = instances.${name}.short;
    }) (builtins.map (service: instances.${service}.name) services);

  instanceList = [
    instances.forgejo.name
    instances.jellyfin.name
    instances.mastodon.name
    instances.vaultwarden.name
    instances.firefly-iii.name
  ];
  bookmarkConfigs = [
    {
      suffix = "(Remote)";
      urlTemplate = name: "https://${instances.${name}.domains.url0}";
      services = instanceList;
    }
    {
      suffix = "(Server)";
      urlTemplate =
        name: "http://${instances.${name}.interface.ip}:${builtins.toString instances.${name}.ports.port0}";
      services = instanceList;
    }
    {
      suffix = "(Desktop)";
      urlTemplate = name: "http://${mars.ip.address0}:${builtins.toString instances.${name}.ports.port0}";
      services = [
        instances.ollama.name
        instances.syncthing.name
      ];
    }
  ];

  generatedBookmarks = builtins.concatLists (builtins.map makeBookmarks bookmarkConfigs);

  flatBookmarks = [
    {
      name = instances.web.label;
      url = "http://${instances.web.localhost.address2}";
      tags = instances.web.tags;
      keyword = instances.web.label;
    }
  ];
in
{
  name = "Self Hosted";
  toolbar = false;
  bookmarks = flatBookmarks ++ generatedBookmarks;
}
