{
  lib,
  config,
  pkgs,
  inputs,
  options,
  ...
}:
let
  cfg = config.firefoxApp;
  # Check if stylix home-manager module is loaded by checking for the firefox target option
  # Only check options (not config) to avoid infinite recursion during module evaluation
  hasStylixFirefox =
    (options ? stylix) && (options.stylix ? targets) && (options.stylix.targets ? firefox);
in
{
  # imports = [
  #     inputs.nur.hmModules.nur
  # ];

  options.firefoxApp = {
    enable = lib.mkEnableOption "enable firefox browser";
  };
  config = lib.mkIf cfg.enable (
    lib.mkMerge (
      [
        # Firefox configuration
        {
          programs.firefox = {
            # Add pipewire support
            # package = (pkgs.wrapFirefox (pkgs.firefox-unwrapped.override { pipewireSupport = true;}) {});
            enable = true;
            profiles = {
              default = {
                id = 0;
                name = "default";
                isDefault = true;
                extensions = {
                  force = true;
                  packages = with pkgs; [
                    nur.repos.rycee.firefox-addons.darkreader
                    nur.repos.rycee.firefox-addons.keepassxc-browser
                    nur.repos.crazazy.firefox-addons.ublock-origin
                  ];
                };
                search = {
                  # Replace default firefox search engine config with this one
                  force = true;
                  default = "Kagi";
                  engines = {
                    "Kagi" = {
                      urls = [
                        {
                          template = "https://kagi.com/search?q={searchTerms}";
                        }
                      ];
                    };
                    "Nix Packages" = {
                      urls = [
                        {
                          template = "https://search.nixos.org/packages";
                          params = [
                            {
                              name = "type";
                              value = "packages";
                            }
                            {
                              name = "query";
                              value = "{searchTerms}";
                            }
                          ];
                        }
                      ];
                      icon = "${pkgs.nixos-icons}/share/icons/hicolor/scalable/apps/nix-snowflake.svg";
                      definedAliases = [ "@np" ];
                    };
                    "NixOS Wiki" = {
                      urls = [
                        {
                          template = "https://wiki.nixos.org/w/index.php";
                          params = [
                            {
                              name = "search";
                              value = "{searchTerms}";
                            }
                          ];
                        }
                      ];
                      icon = "${pkgs.nixos-icons}/share/icons/hicolor/scalable/apps/nix-snowflake.svg";
                      definedAliases = [ "@nix" ];
                    };
                    "MySQL Docs" = {
                      urls = [
                        {
                          template = "https://dev.mysql.com/doc/search";
                          params = [
                            {
                              name = "d";
                              value = "371";
                            }
                            {
                              name = "p";
                              value = "1";
                            }
                            {
                              name = "q";
                              value = "{searchTerms}";
                            }
                          ];
                        }
                      ];
                      definedAliases = [ "@mysql" ];
                    };
                    "SQLite Docs" = {
                      urls = [
                        {
                          template = "https://www.sqlite.org/search?s=d&q=strict";
                          params = [
                            {
                              name = "s";
                              value = "d";
                            }
                            {
                              name = "q";
                              value = "{searchTerms}";
                            }
                          ];
                        }
                      ];
                      definedAliases = [ "@sqlite" ];
                    };
                  };
                };
                settings = {
                  "browser.startup.blankWindow" = true;
                  "browser.startup.homepage" = "https://www.kagi.com";
                  "browser.search.region" = "US";
                  "browser.search.isUS" = true;
                  "distribution.searchplugins.defaultLocale" = "en-US";
                  "general.useragent.locale" = "en-US";
                  "extensions.activeThemeID" = "{831b8843-7251-4306-9521-e688f18b4aeb}";
                  "privacy.trackingprotection.enable" = true;
                  "privacy.trackingprotection.emailtracking.enable" = true;
                  "privacy.trackingprotection.socialtracking.enable" = true;
                  "privacy.clearOnShutdown.cookies" = true;
                  "browser.safebrowsing.passwords.enabled" = false;
                  "browser.migrate.interactions.passwords" = false;
                  "pref.privacy.disable_button.view_passwords" = false;
                  "signon.rememberSignon" = false;
                  "extensions.formautofill.creditCards.enabled" = false;
                  "extensions.formautofill.addresses.enabled" = false;
                  "browser.toolbars.bookmarks.visibility" = "always";
                  # For applying userContent.css - not being used
                  # "toolkit.legacyUserProfileCustomizations.stylesheets" = true;
                  # "browser.newtabpage.pinned" = [{
                  #   title = "NixOS";
                  #   url = "https://nixos.org";
                  # }];
                };
              };
            };
          };
        }
      ]
      ++ lib.optional hasStylixFirefox {
        # Stylix integration (only if stylix module is loaded)
        # Uses mkIf to check autoEnable at runtime, avoiding infinite recursion
        stylix.targets.firefox.profileNames = lib.mkIf config.stylix.autoEnable [ "default" ];
      }
    )
  );
}
