{ config, pkgs, lib, ... }: let
  mautrix-discord-user = config.systemd.services.mautrix-discord.serviceConfig.User;
in {
  sops.secrets.mautrix-discord = {
    owner = mautrix-discord-user;
  };
  services.postgresql = {
    enable = true;
    ensureDatabases = [
      mautrix-discord-user
    ];
    ensureUsers = [
      {
        name = "${mautrix-discord-user}";
        ensureDBOwnership = true;
      }
    ];
  };
  services.mautrix-discord = {
    enable = true;
    # Secrets stored in environmentFile
    settings = {
      logging = {
        min_level = "debug";
        writers = [{
          type = "stdout";
          format = "pretty-colored";
        } {
          type = "file";
          format = "json";
          filename = "./logs/mautrix-discord.log";
          max_size = 100;
          max_backups = 10;
          compress = true;
        }];           
      };
      homeserver = {
        # The address that this appservice can use to connect to the homeserver.
        address = "https://glia.club";
        # The domain of the homeserver (also known as server_name, used for MXIDs, etc).
        domain = "glia.club";

        # What software is the homeserver running?
        # Standard Matrix homeservers like Synapse, Dendrite and Conduit should just use "standard" here.
        software = "standard";
        # The URL to push real-time bridge status to.
        # If set, the bridge will make POST requests to this URL whenever a user's discord connection state changes.
        # The bridge will use the appservice as_token to authorize requests.
        status_endpoint = null;
        # Endpoint for reporting per-message status.
        message_send_checkpoint_endpoint = null;
        # Does the homeserver support https://github.com/matrix-org/matrix-spec-proposals/pull/2246?
        async_media = false;

        # Should the bridge use a websocket for connecting to the homeserver?
        # The server side is currently not documented anywhere and is only implemented by mautrix-wsproxy,
        # mautrix-asmux (deprecated), and hungryserv (proprietary).
        websocket = false;
        # How often should the websocket be pinged? Pinging will be disabled if this is zero.
        ping_interval_seconds = 0;
      };
      bridge = {
        # Localpart template of MXIDs for Discord users.
        # {{.}} is replaced with the internal ID of the Discord user.
        username_template = "bridge_discord_{{.}}";
        # Displayname template for Discord users. This is also used as the room name in DMs if private_chat_portal_meta is enabled.
        # Available variables:
        #   .ID - Internal user ID
        #   .Username - Legacy display/username on Discord
        #   .GlobalName - New displayname on Discord
        #   .Discriminator - The 4 numbers after the name on Discord
        #   .Bot - Whether the user is a bot
        #   .System - Whether the user is an official system user
        #   .Webhook - Whether the user is a webhook and is not an application
        #   .Application - Whether the user is an application
        displayname_template = "{{if .Webhook}}Webhook{{else}}{{or .GlobalName .Username}}{{if .Bot}} (bot){{end}}{{end}} (Discord DM)";
        # Displayname template for Discord channels (bridged as rooms, or spaces when type=4).
        # Available variables:
        #   .Name - Channel name, or user displayname (pre-formatted with displayname_template) in DMs.
        #   .ParentName - Parent channel name (used for categories).
        #   .GuildName - Guild name.
        #   .NSFW - Whether the channel is marked as NSFW.
        #   .Type - Channel type (see values at https://github.com/bwmarrin/discordgo/blob/v0.25.0/structs.go#L251-L267)
        channel_name_template = "{{if or (eq .Type 3) (eq .Type 4)}}{{.Name}}{{else}}#{{.Name}}{{end}} (Discord)";
        # Displayname template for Discord guilds (bridged as spaces).
        # Available variables:
        #   .Name - Guild name
        guild_name_template = "{{.Name}} (Discord)";
        # Whether to explicitly set the avatar and room name for private chat portal rooms.
        # If set to `default`, this will be enabled in encrypted rooms and disabled in unencrypted rooms.
        # If set to `always`, all DM rooms will have explicit names and avatars set.
        # If set to `never`, DM rooms will never have names and avatars set.
        private_chat_portal_meta = "default";

        # Publicly accessible base URL that Discord can use to reach the bridge, used for avatars in relay mode.
        # If not set, avatars will not be bridged. Only the /mautrix-discord/avatar/{server}/{id}/{hash} endpoint is used on this address.
        # This should not have a trailing slash, the endpoint above will be appended to the provided address.
        public_address = "https://discord.bridge.matrix.glia.club";
        # A random key used to sign the avatar URLs. The bridge will only accept requests with a valid signature.
        avatar_proxy_key = "generate";

        portal_message_buffer = 128;

        # Number of private channel portals to create on bridge startup.
        # Other portals will be created when receiving messages.
        startup_private_channel_create_limit = 5;
        # Should the bridge send a read receipt from the bridge bot when a message has been sent to Discord?
        delivery_receipts = false;
        # Whether the bridge should send the message status as a custom com.beeper.message_send_status event.
        message_status_events = false;
        # Whether the bridge should send error notices via m.notice events when a message fails to bridge.
        message_error_notices = true;
        # Should the bridge use space-restricted join rules instead of invite-only for guild rooms?
        # This can avoid unnecessary invite events in guild rooms when members are synced in.
        restricted_rooms = false;
        # Should the bridge automatically join the user to threads on Discord when the thread is opened on Matrix?
        # This only works with clients that support thread read receipts (MSC3771 added in Matrix v1.4).
        autojoin_thread_on_open = true;
        # Should inline fields in Discord embeds be bridged as HTML tables to Matrix?
        # Tables aren't supported in all clients, but are the only way to emulate the Discord inline field UI.
        embed_fields_as_tables = true;
        # Should guild channels be muted when the portal is created? This only meant for single-user instances,
        # it won't mute it for all users if there are multiple Matrix users in the same Discord guild.
        mute_channels_on_create = false;
        # Should the bridge update the m.direct account data event when double puppeting is enabled.
        # Note that updating the m.direct event is not atomic (except with mautrix-asmux)
        # and is therefore prone to race conditions.
        sync_direct_chat_list = false;
        # Set this to true to tell the bridge to re-send m.bridge events to all rooms on the next run.
        # This field will automatically be changed back to false after it, except if the config file is not writable.
        resend_bridge_info = false;
        # Should incoming custom emoji reactions be bridged as mxc:// URIs?
        # If set to false, custom emoji reactions will be bridged as the shortcode instead, and the image won't be available.
        custom_emoji_reactions = true;
        # Should the bridge attempt to completely delete portal rooms when a channel is deleted on Discord?
        # If true, the bridge will try to kick Matrix users from the room. Otherwise, the bridge only makes ghosts leave.
        delete_portal_on_channel_delete = false;
        # Should the bridge delete all portal rooms when you leave a guild on Discord?
        # This only applies if the guild has no other Matrix users on this bridge instance.
        delete_guild_on_leave = true;
        # Whether or not created rooms should have federation enabled.
        # If false, created portal rooms will never be federated.
        federate_rooms = false;
        # Prefix messages from webhooks with the profile info? This can be used along with a custom displayname_template
        # to better handle webhooks that change their name all the time (like ones used by bridges).
        #
        # This will use the fallback mode in MSC4144, which means clients that support MSC4144 will not show the prefix
        # (and will instead show the name and avatar as the message sender).
        prefix_webhook_messages = true;
        # Bridge webhook avatars?
        enable_webhook_avatars = true;
        # Should the bridge upload media to the Discord CDN directly before sending the message when using a user token,
        # like the official client does? The other option is sending the media in the message send request as a form part
        # (which is always used by bots and webhooks).
        use_discord_cdn_upload = true;
        # Proxy for Discord connections
        proxy = "";
        # Should mxc uris copied from Discord be cached?
        # This can be `never` to never cache, `unencrypted` to only cache unencrypted mxc uris, or `always` to cache everything.
        # If you have a media repo that generates non-unique mxc uris, you should set this to never.
        cache_media = "unencrypted";
        # Settings for converting Discord media to custom mxc:// URIs instead of reuploading.
        # More details can be found at https://docs.mau.fi/bridges/go/discord/direct-media.html
        direct_media = {
          # Should custom mxc:// URIs be used instead of reuploading media?
          enabled = true;
          # The server name to use for the custom mxc:// URIs.
          # This server name will effectively be a real Matrix server, it just won't implement anything other than media.
          # You must either set up .well-known delegation from this domain to the bridge, or proxy the domain directly to the bridge.
          server_name = "discord.bridge.matrix.glia.club";
          # Optionally a custom .well-known response. This defaults to `server_name:443`
          # well_known_response = "";
          # The bridge supports MSC3860 media download redirects and will use them if the requester supports it.
          # Optionally, you can force redirects and not allow proxying at all by setting this to false.
          allow_proxy = true;
        };
        # Settings for converting animated stickers.
        animated_sticker = {
          # Format to which animated stickers should be converted.
          # disable - No conversion, send as-is (lottie JSON)
          # png - converts to non-animated png (fastest)
          # gif - converts to animated gif
          # webm - converts to webm video, requires ffmpeg executable with vp9 codec and webm container support
          # webp - converts to animated webp, requires ffmpeg executable with webp codec/container support
          target = "webp";
          # Arguments for converter. All converters take width and height.
          args = {
            width = 320;
            height = 320;
            fps = 25; # only for webm, webp and gif (2, 5, 10, 20 or 25 recommended)
          };
        };
        # Servers to always allow double puppeting from
        double_puppet_server_map = {
          "glia.club" = "https://glia.club";
        };
        # Allow using double puppeting from any server with a valid client .well-known file.
        double_puppet_allow_discovery = false;
        # Shared secrets for https://github.com/devture/matrix-synapse-shared-secret-auth
        #
        # If set, double puppeting will be enabled automatically for local users
        # instead of users having to find an access token and run `login-matrix`
        # manually.
        login_shared_secret_map = {
          "glia.club" = "as_token:$MAUTRIX_DISCORD_DOUBLE_PUPPET";
        };

        # The prefix for commands. Only required in non-management rooms.
        command_prefix = "!discord";
        # Messages sent upon joining a management room.
        # Markdown is supported. The defaults are listed below.
        management_room_text = {
          # Sent when joining a room.
          welcome = "Hello, I'm a Discord bridge bot.";
          # Sent when joining a management room and the user is already logged in.
          welcome_connected = "Use `help` for help.";
          # Sent when joining a management room and the user is not logged in.
          welcome_unconnected = "Use `help` for help or `login` to log in.";
          # Optional extra text sent when joining a management room.
          additional_help = "";
        };

        # Settings for backfilling messages.
        backfill = {
          # Limits for forward backfilling.
          forward_limits = {
            # Initial backfill (when creating portal). 0 means backfill is disabled.
            # A special unlimited value is not supported, you must set a limit. Initial backfill will
            # fetch all messages first before backfilling anything, so high limits can take a lot of time.
            initial = {
              dm = 5000;
              channel = 5000;
              thread = 5000;
            };
            # Missed message backfill (on startup).
            # 0 means backfill is disabled, -1 means fetch all messages since last bridged message.
            # When using unlimited backfill (-1), messages are backfilled as they are fetched.
            # With limits, all messages up to the limit are fetched first and backfilled afterwards.
            missed = {
              dm = -1;
              channel = -1;
              thread = -1;
            };
            # Maximum members in a guild to enable backfilling. Set to -1 to disable limit.
            # This can be used as a rough heuristic to disable backfilling in channels that are too active.
            # Currently only applies to missed message backfill.
            max_guild_members = -1;
          };
        };

        # End-to-bridge encryption support options.
        #
        # See https://docs.mau.fi/bridges/general/end-to-bridge-encryption.html for more info.
        encryption = {
          # Allow encryption, work in group chat rooms with e2ee enabled
          allow = false;
          # Default to encryption, force-enable encryption in all portals the bridge creates
          # This will cause the bridge bot to be in private chats for the encryption to work properly.
          default = false;
          # Whether to use MSC2409/MSC3202 instead of /sync long polling for receiving encryption-related data.
          # Changing this option requires updating the appservice registration file.
          appservice = false;
          # Whether to use MSC4190 instead of appservice login to create the bridge bot device.
          # Requires the homeserver to support MSC4190 and the device masquerading parts of MSC3202.
          # Only relevant when using end-to-bridge encryption, required when using encryption with next-gen auth (MSC3861).
          # Changing this option requires updating the appservice registration file.
          msc4190 = false;
          # Require encryption, drop any unencrypted messages.
          require = false;
          # Enable key sharing? If enabled, key requests for rooms where users are in will be fulfilled.
          # You must use a client that supports requesting keys from other users to use this feature.
          allow_key_sharing = false;
          # Should users mentions be in the event wire content to enable the server to send push notifications?
          plaintext_mentions = false;
          # Options for deleting megolm sessions from the bridge.
          delete_keys = {
            # Beeper-specific: delete outbound sessions when hungryserv confirms
            # that the user has uploaded the key to key backup.
            delete_outbound_on_ack = false;
            # Don't store outbound sessions in the inbound table.
            dont_store_outbound = false;
            # Ratchet megolm sessions forward after decrypting messages.
            ratchet_on_decrypt = false;
            # Delete fully used keys (index >= max_messages) after decrypting messages.
            delete_fully_used_on_decrypt = false;
            # Delete previous megolm sessions from same device when receiving a new one.
            delete_prev_on_new_session = false;
            # Delete megolm sessions received from a device when the device is deleted.
            delete_on_device_delete = false;
            # Periodically delete megolm sessions when 2x max_age has passed since receiving the session.
            periodically_delete_expired = false;
            # Delete inbound megolm sessions that don't have the received_at field used for
            # automatic ratcheting and expired session deletion. This is meant as a migration
            # to delete old keys prior to the bridge update.
            delete_outdated_inbound = false;
          };
          # What level of device verification should be required from users?
          #
          # Valid levels:
          #   unverified - Send keys to all device in the room.
          #   cross-signed-untrusted - Require valid cross-signing, but trust all cross-signing keys.
          #   cross-signed-tofu - Require valid cross-signing, trust cross-signing keys on first use (and reject changes).
          #   cross-signed-verified - Require valid cross-signing, plus a valid user signature from the bridge bot.
          #                           Note that creating user signatures from the bridge bot is not currently possible.
          #   verified - Require manual per-device verification
          #              (currently only possible by modifying the `trust` column in the `crypto_device` database table).
          verification_levels = {
            # Minimum level for which the bridge should send keys to when bridging messages from WhatsApp to Matrix.
            receive = "unverified";
            # Minimum level that the bridge should accept for incoming Matrix messages.
            send = "unverified";
            # Minimum level that the bridge should require for accepting key requests.
            share = "cross-signed-tofu";
          };
          # Options for Megolm room key rotation. These options allow you to
          # configure the m.room.encryption event content. See:
          # https://spec.matrix.org/v1.3/client-server-api/#mroomencryption for
          # more information about that event.
          rotation = {
            # Enable custom Megolm room key rotation settings. Note that these
            # settings will only apply to rooms created after this option is
            # set.
            enable_custom = false;
            # The maximum number of milliseconds a session should be used
            # before changing it. The Matrix spec recommends 604800000 (a week)
            # as the default.
            milliseconds = 604800000;
            # The maximum number of messages that should be sent with a given a
            # session before changing it. The Matrix spec recommends 100 as the
            # default.
            messages = 100;

            # Disable rotating keys when a user's devices change?
            # You should not enable this option unless you understand all the implications.
            disable_device_change_key_rotation = false;
          };
        };

        # Settings for provisioning API
        provisioning = {
          # Prefix for the provisioning API paths.
          prefix = "/_matrix/provision";
          # Shared secret for authentication. If set to "generate", a random secret will be generated,
          # or if set to "disable", the provisioning API will be disabled.
          shared_secret = "generate";
          # Enable debug API at /debug with provisioning authentication.
          debug_endpoints = false;
        };

        # Permissions for using the bridge.
        # Permitted values:
        #    relay - Talk through the relaybot (if enabled), no access otherwise
        #     user - Access to use the bridge to chat with a Discord account.
        #    admin - User level and some additional administration tools
        # Permitted keys:
        #        * - All Matrix users
        #   domain - All users on that homeserver
        #     mxid - Specific user
        permissions = {
          "*" = "relay";
          "glia.club" = "user";
          "@admin:glia.club" = "admin";
          "@cyborgpotato:glia.club" = "admin";
        };
      };
      appservice = {
        # The address that the homeserver can use to connect to this appservice.
        address = "http://localhost:${toString config.services.mautrix-discord.settings.appservice.port}";

        # The hostname and port where this appservice should listen.
        hostname = "0.0.0.0";
        port = 7193;

        # Database config.
        # See definition at top of file
        database = {
          # The database type. "sqlite3-fk-wal" and "postgres" are supported.
          type = "postgres";
          # The database URI.
          #   SQLite: A raw file path is supported, but `file:<path>?_txlock=immediate` is recommended.
          #           https://github.com/mattn/go-sqlite3#connection-string
          #   Postgres: Connection string. For example, postgres://user:password@host/database?sslmode=disable
          #             To connect via Unix socket, use something like postgres:///dbname?host=/var/run/postgresql
          uri = "postgres:///${mautrix-discord-user}?host=/var/run/postgresql";
          # Maximum number of connections. Mostly relevant for Postgres.
          max_open_conns = 20;
          max_idle_conns = 2;
          # Maximum connection idle time and lifetime before they're closed. Disabled if null.
          # Parsed with https://pkg.go.dev/time#ParseDuration
          max_conn_idle_time = null;
          max_conn_lifetime = null;
        };

        # The unique ID of this appservice.
        id = "discord";
        # Appservice bot details.
        bot = {
          # Username of the appservice bot.
          username = "discordbot";
          # Display name and avatar for bot. Set to "remove" to remove display name/avatar, leave empty
          # to leave display name/avatar as-is.
          displayname = "Discord bridge bot";
          avatar = "mxc://maunium.net/nIdEykemnwdisvHbpxflpDlC";
        };

        # Whether or not to receive ephemeral events via appservice transactions.
        # Requires MSC2409 support (i.e. Synapse 1.22+).
        ephemeral_events = true;

        # Should incoming events be handled asynchronously?
        # This may be necessary for large public instances with lots of messages going through.
        # However, messages will not be guaranteed to be bridged in the same order they were sent in.
        async_transactions = false;

        # Authentication tokens for AS <-> HS communication. Autogenerated; do not modify.
        as_token = "$MAUTRIX_DISCORD_APPSERVICE_AS_TOKEN";
        hs_token = "$MAUTRIX_DISCORD_APPSERVICE_HS_TOKEN";
      };
    };
    serviceDependencies = [ config.services.mautrix-discord.registrationServiceUnit ]
                          ++ (lib.lists.optional config.services.matrix-synapse.enable config.services.matrix-synapse.serviceUnit)
                          ++ (lib.lists.optional config.services.matrix-conduit.enable "matrix-conduit.service")
                          ++ (lib.lists.optional config.services.matrix-continuwuity.enable "matrix-continuwuity.service")
                          ++ (lib.lists.optional config.services.matrix-tuwunel.enable "matrix-tuwunel.service")
                          ++ (lib.lists.optional config.services.dendrite.enable "dendrite.service");
    environmentFile = config.sops.secrets.mautrix-discord.path;
  };
  services.caddy.virtualHosts = {
    "${config.services.mautrix-discord.settings.bridge.direct_media.server_name}" = {
      extraConfig = ''
        reverse_proxy localhost:${toString config.services.mautrix-discord.settings.appservice.port}
      '';
    };
  };
}
