{
  lib,
  config,
  ...
}: let
  inherit (lib) mkEnableOption mkIf;
  cfg = config.horseman.apps.visual.firefox;

  lock-false = {
    Value = false;
    Status = "locked";
  };
  lock-true = {
    Value = true;
    Status = "locked";
  };
in {
  options = {
    horseman.apps.visual.firefox = {
      enable = mkEnableOption "Firefox";
    };
  };

  config = mkIf cfg.enable {
    programs.firefox = {
      enable = true;
      policies = {
        DisableTelemetry = true;
        DisableFirefoxStudies = true;
        DisablePocket = true;
        DisableAccounts = true;
        DisableFirefoxScreenshots = true;
        DisplayBookmarksToolbar = "never";
        DontCheckDefaultBrowser = true;
        GenerativeAI = false;
        PasswordManagerEnabled = false;

        SearchEngines = {
          Default = "DuckDuckGo";
          Add = [
            {
              Name = "DuckDuckGo";
              URLTemplate = "https://duckduckgo.com/?q={searchTerms}";
              Alias = "@ddg";
            }
            {
              Name = "NixOS Packages";
              URLTemplate = "https://search.nixos.org/packages?query={searchTerms}";
              Alias = "@pack";
              IconURL = "https://search.nixos.org/favicon-96x96.png";
            }
            {
              Name = "NixOS Options";
              URLTemplate = "https://search.nixos.org/options?query={searchTerms}";
              Alias = "@opt";
              IconURL = "https://search.nixos.org/favicon-96x96.png";
            }
          ];
        };

        ExtensionSettings = {
          "*".installation_mode = "blocked";
          # uBlock Origin:
          "uBlock0@raymondhill.net" = {
            install_url = "https://addons.mozilla.org/firefox/downloads/latest/ublock-origin/latest.xpi";
            installation_mode = "force_installed";
            private_browsing = true;
          };
          # Dark Reader
          "addon@darkreader.org" = {
            install_url = "https://addons.mozilla.org/firefox/downloads/latest/darkreader/latest.xpi";
            installation_mode = "force_installed";
          };
          # Advent of Code delta-rank
          "{78b07db2-25ed-4088-9c92-5c5dcfca3c3c}" = {
            install_url = "https://addons.mozilla.org/firefox/downloads/latest/advent-of-code-delta-score/latest.xpi";
            installation_mode = "force_installed";
          };
          # SponsorBlock
          "sponsorBlocker@ajay.app" = {
            install_url = "https://addons.mozilla.org/firefox/downloads/latest/sponsorblock/latest.xpi";
            installation_mode = "force_installed";
          };
          # Bitwarden
          "{446900e4-71c2-419f-a6a7-df9c091e268b}" = {
            install_url = "https://addons.mozilla.org/firefox/downloads/latest/bitwarden-password-manager/latest.xpi";
            installation_mode = "force_installed";
          };
          # Cookie Blocker
          "idcac-pub@guus.ninja" = {
            install_url = "https://addons.mozilla.org/firefox/downloads/latest/istilldontcareaboutcookies/latest.xpi";
            installation_mode = "force_installed";
            private_browsing = true;
          };
        };

        Cookies = {
          Behaviour = "reject-foreign"; # No third party cookies
          BehaviourPrivateBrowsing = "reject";
        };

        WebsiteFilter.Block = ["https://chatgpt.com"];

        FirefoxHome = {
          "SponsoredTopSites" = false;
          "SponsoredPocket" = false;
          "SponsoredStories" = false;
        };

        FirefoxSuggest = {
          SponsoredSuggestions = false;
          ImproveSuggest = false;
        };

        EnableTrackingProtection = {
          Value = true;
          Locked = true;
        };

        Preferences = {
          general = {
            autoScroll = true;
          };
          "browser.newtabpage.activity-stream.showSponsored" = lock-false;
          "browser.newtabpage.activity-stream.system.showSponsored" = lock-false;
          "browser.newtabpage.activity-stream.showSponsoredTopSites" = lock-false;

          # Container tabs
          "privacy.userContext.enabled" = lock-true;
          "privacy.userContext.ui.enabled" = lock-true;
        };
      };
    };
  };
}
