/*
* Copyright (c) Microsoft Corporation.  All rights reserved.
 *
 * documentdb_gateway_core/src/requests/request_type.rs
 *
 *-------------------------------------------------------------------------
 */

use std::{fmt, str::FromStr};

use crate::error::{DocumentDBError, ErrorCode, Result};

#[derive(PartialEq, Eq, Debug, Copy, Clone)]
pub enum RequestType {
    AbortTransaction,
    Aggregate,
    AppendOplogNote,
    ApplyOps,
    Authenticate,
    AuthSchemaUpgrade,
    AvailableQueryOptions,
    BalancerStart,
    BalancerStatus,
    BalancerStop,
    BuildInfo,
    CheckShardingIndex,
    CleanupOrphaned,
    Clone,
    CloneCatalogData,
    CloneCollection,
    CloneCollectionAsCapped,
    CloneCollectionOptionsFromPrimaryShard,
    CollMod,
    CollStats,
    CommitTransaction,
    Compact,
    ConfigsvrAddShard,
    ConfigsvrAddShardToZone,
    ConfigsvrBalancerStart,
    ConfigsvrBalancerStatus,
    ConfigsvrBalancerStop,
    ConfigsvrCommitChunkMerge,
    ConfigsvrCommitChunkMigration,
    ConfigsvrCommitChunkSplit,
    ConfigsvrCommitMovePrimary,
    ConfigsvrCreateCollection,
    ConfigsvrCreateDatabase,
    ConfigsvrDropCollection,
    ConfigsvrDropDatabase,
    ConfigsvrEnableSharding,
    ConfigsvrMoveChunk,
    ConfigsvrMovePrimary,
    ConfigsvrRemoveShard,
    ConfigsvrRemoveShardFromZone,
    ConfigsvrShardCollection,
    ConfigsvrUpdateZoneKeyRange,
    ConnectionStatus,
    ConnPoolStats,
    ConnPoolSync,
    ConvertToCapped,
    Copydb,
    Copydbgetnonce,
    Copydbsaslstart,
    Count,
    Create,
    CreateIndex,
    CreateIndexes,
    CreateRole,
    CreateUniqueOrCompoundIndexOnNestedPath,
    CreateUser,
    CurrentOp,
    CurrentOpCtx,
    CustomAction,
    DataSize,
    DbHash,
    DbStats,
    Delete,
    DeleteIndexes,
    DiagLogging,
    Distinct,
    DriverOIDTest,
    Drop,
    DropAllRolesFromDatabase,
    DropAllUsersFromDatabase,
    DropDatabase,
    DropIndexes,
    DropRole,
    DropUser,
    EnableSharding,
    EndSessions,
    Eval,
    Explain,
    Features,
    Filemd5,
    Find,
    FindAndModify,
    FlushDatabaseCacheUpdates,
    FlushRoutingTableCacheUpdates,
    Forceerror,
    Fsync,
    FsyncUnlock,
    GeoNear,
    GeoSearch,
    GetCmdLineOpts,
    GetDatabaseVersion,
    GetDefaultRWConcern,
    GetDiagnosticData,
    GetFreeMonitoringStatus,
    GetLastError,
    GetLastRequestStatistics,
    GetLog,
    GetMore,
    GetNextSessionMods,
    GetNonce,
    GetParameter,
    GetPrevError,
    GetShardMap,
    GetShardVersion,
    GetUserCacheGeneration,
    GrantPrivilegesToRole,
    GrantRolesToRole,
    GrantRolesToUser,
    Group,
    Handshake,
    Hello,
    HostInfo,
    Insert,
    InvalidateUserCache,
    Isdbgrid,
    IsMaster,
    IsSelf,
    KillAllSessions,
    KillAllSessionsByPattern,
    KillCursors,
    KillOp,
    KillSessions,
    ListCollections,
    ListCommands,
    ListDatabases,
    ListIndexes,
    ListShards,
    LockInfo,
    Logout,
    LogRotate,
    MapReduce,
    Mapreduceshardedfinish,
    MergeAuthzCollections,
    MergeChunks,
    MigrateClone,
    MoveChunk,
    MoveCollection,
    MovePrimary,
    ParallelCollectionScan,
    Ping,
    PlanCacheClear,
    PlanCacheClearFilters,
    PlanCacheListFilters,
    PlanCacheListPlans,
    PlanCacheListQueryShapes,
    PlanCacheSetFilter,
    PrepareTransaction,
    Profile,
    RecvChunkAbort,
    RecvChunkCommit,
    RecvChunkStart,
    RecvChunkStatus,
    RefreshSessions,
    RefreshSessionsInternal,
    ReIndex,
    RenameCollection,
    RepairCursor,
    RepairDatabase,
    ReplSetAbortPrimaryCatchUp,
    ReplSetDeclareElectionWinner,
    ReplSetElect,
    ReplSetFreeze,
    ReplSetFresh,
    ReplSetGetConfig,
    ReplSetGetRBID,
    ReplSetGetStatus,
    ReplSetHeartbeat,
    ReplSetInitiate,
    ReplSetMaintenance,
    ReplSetReconfig,
    ReplSetRequestVotes,
    ReplSetResizeOplog,
    ReplSetStepDown,
    ReplSetStepUp,
    ReplSetSyncFrom,
    ReplSetUpdatePosition,
    ResetError,
    ReshardCollection,
    ReSync,
    RevokePrivilegesFromRole,
    RevokeRolesFromRole,
    RevokeRolesFromUser,
    RolesInfo,
    SaslContinue,
    SaslStart,
    ServerStatus,
    SetFeatureCompatibilityVersion,
    SetFreeMonitoring,
    SetParameter,
    SetShardVersion,
    ShardCollection,
    ShardConnPoolStats,
    ShardingState,
    ShardsvrShardCollection,
    Shutdown,
    SplitChunk,
    SplitVector,
    StartSession,
    Top,
    Touch,
    TransferMods,
    UnsetSharding,
    UnshardCollection,
    Update,
    UpdateRole,
    UpdateUser,
    UsersInfo,
    Validate,
    WhatsMyUri,
    Writebacklisten,
}

impl RequestType {
    #[must_use]
    pub const fn handle_with_auth(self) -> bool {
        matches!(&self, Self::Logout | Self::SaslContinue | Self::SaslStart)
    }

    #[must_use]
    pub const fn allowed_unauthorized(self) -> bool {
        matches!(
            &self,
            Self::IsMaster | Self::Hello | Self::Ping | Self::BuildInfo
        )
    }

    #[expect(
        clippy::too_many_lines,
        reason = "The enum has all of these variants, so it's expected to have long to string method"
    )]
    #[must_use]
    pub const fn to_command_str(self) -> &'static str {
        match self {
            Self::AbortTransaction => "abortTransaction",
            Self::Aggregate => "aggregate",
            Self::AppendOplogNote => "appendOplogNote",
            Self::ApplyOps => "applyOps",
            Self::Authenticate => "authenticate",
            Self::AuthSchemaUpgrade => "authSchemaUpgrade",
            Self::AvailableQueryOptions => "availableQueryOptions",
            Self::BalancerStart => "balancerStart",
            Self::BalancerStatus => "balancerStatus",
            Self::BalancerStop => "balancerStop",
            Self::BuildInfo => "buildInfo",
            Self::CheckShardingIndex => "checkShardingIndex",
            Self::CleanupOrphaned => "cleanupOrphaned",
            Self::Clone => "clone",
            Self::CloneCatalogData => "_cloneCatalogData",
            Self::CloneCollection => "cloneCollection",
            Self::CloneCollectionAsCapped => "cloneCollectionAsCapped",
            Self::CloneCollectionOptionsFromPrimaryShard => {
                "_cloneCollectionOptionsFromPrimaryShard"
            }
            Self::CollMod => "collMod",
            Self::CollStats => "collStats",
            Self::CommitTransaction => "commitTransaction",
            Self::Compact => "compact",
            Self::ConfigsvrAddShard => "_configsvrAddShard",
            Self::ConfigsvrAddShardToZone => "_configsvrAddShardToZone",
            Self::ConfigsvrBalancerStart => "_configsvrBalancerStart",
            Self::ConfigsvrBalancerStatus => "_configsvrBalancerStatus",
            Self::ConfigsvrBalancerStop => "_configsvrBalancerStop",
            Self::ConfigsvrCommitChunkMerge => "_configsvrCommitChunkMerge",
            Self::ConfigsvrCommitChunkMigration => "_configsvrCommitChunkMigration",
            Self::ConfigsvrCommitChunkSplit => "_configsvrCommitChunkSplit",
            Self::ConfigsvrCommitMovePrimary => "_configsvrCommitMovePrimary",
            Self::ConfigsvrCreateCollection => "_configsvrCreateCollection",
            Self::ConfigsvrCreateDatabase => "_configsvrCreateDatabase",
            Self::ConfigsvrDropCollection => "_configsvrDropCollection",
            Self::ConfigsvrDropDatabase => "_configsvrDropDatabase",
            Self::ConfigsvrEnableSharding => "_configsvrEnableSharding",
            Self::ConfigsvrMoveChunk => "_configsvrMoveChunk",
            Self::ConfigsvrMovePrimary => "_configsvrMovePrimary",
            Self::ConfigsvrRemoveShard => "_configsvrRemoveShard",
            Self::ConfigsvrRemoveShardFromZone => "_configsvrRemoveShardFromZone",
            Self::ConfigsvrShardCollection => "_configsvrShardCollection",
            Self::ConfigsvrUpdateZoneKeyRange => "_configsvrUpdateZoneKeyRange",
            Self::ConnectionStatus => "connectionStatus",
            Self::ConnPoolStats => "connPoolStats",
            Self::ConnPoolSync => "connPoolSync",
            Self::ConvertToCapped => "convertToCapped",
            Self::Copydb => "copydb",
            Self::Copydbgetnonce => "copydbgetnonce",
            Self::Copydbsaslstart => "copydbsaslstart",
            Self::Count => "count",
            Self::Create => "create",
            Self::CreateIndex => "createIndex",
            Self::CreateIndexes => "createIndexes",
            Self::CreateRole => "createRole",
            Self::CreateUniqueOrCompoundIndexOnNestedPath => {
                "createUniqueOrCompoundIndexOnNestedPath"
            }
            Self::CreateUser => "createUser",
            Self::CurrentOp => "currentOp",
            Self::CurrentOpCtx => "currentOpCtx",
            Self::CustomAction => "customAction",
            Self::DataSize => "dataSize",
            Self::DbHash => "dbHash",
            Self::DbStats => "dbStats",
            Self::Delete => "delete",
            Self::DeleteIndexes => "deleteIndexes",
            Self::DiagLogging => "diagLogging",
            Self::Distinct => "distinct",
            Self::DriverOIDTest => "driverOIDTest",
            Self::Drop => "drop",
            Self::DropAllRolesFromDatabase => "dropAllRolesFromDatabase",
            Self::DropAllUsersFromDatabase => "dropAllUsersFromDatabase",
            Self::DropDatabase => "dropDatabase",
            Self::DropIndexes => "dropIndexes",
            Self::DropRole => "dropRole",
            Self::DropUser => "dropUser",
            Self::EnableSharding => "enableSharding",
            Self::EndSessions => "endSessions",
            Self::Eval => "eval",
            Self::Explain => "explain",
            Self::Features => "features",
            Self::Filemd5 => "filemd5",
            Self::Find => "find",
            Self::FindAndModify => "findAndModify",
            Self::FlushDatabaseCacheUpdates => "_flushDatabaseCacheUpdates",
            Self::FlushRoutingTableCacheUpdates => "_flushRoutingTableCacheUpdates",
            Self::Forceerror => "forceerror",
            Self::Fsync => "fsync",
            Self::FsyncUnlock => "fsyncUnlock",
            Self::GeoNear => "geoNear",
            Self::GeoSearch => "geoSearch",
            Self::GetCmdLineOpts => "getCmdLineOpts",
            Self::GetDatabaseVersion => "getDatabaseVersion",
            Self::GetDefaultRWConcern => "getDefaultRWConcern",
            Self::GetDiagnosticData => "getDiagnosticData",
            Self::GetFreeMonitoringStatus => "getFreeMonitoringStatus",
            Self::GetLastError => "getLastError",
            Self::GetLastRequestStatistics => "getLastRequestStatistics",
            Self::GetLog => "getLog",
            Self::GetMore => "getMore",
            Self::GetNextSessionMods => "_getNextSessionMods",
            Self::GetNonce => "getNonce",
            Self::GetParameter => "getParameter",
            Self::GetPrevError => "getPrevError",
            Self::GetShardMap => "getShardMap",
            Self::GetShardVersion => "getShardVersion",
            Self::GetUserCacheGeneration => "_getUserCacheGeneration",
            Self::GrantPrivilegesToRole => "grantPrivilegesToRole",
            Self::GrantRolesToRole => "grantRolesToRole",
            Self::GrantRolesToUser => "grantRolesToUser",
            Self::Group => "group",
            Self::Handshake => "handshake",
            Self::Hello => "hello",
            Self::HostInfo => "hostInfo",
            Self::Insert => "insert",
            Self::InvalidateUserCache => "invalidateUserCache",
            Self::Isdbgrid => "isdbgrid",
            Self::IsMaster => "isMaster",
            Self::IsSelf => "_isSelf",
            Self::KillAllSessions => "killAllSessions",
            Self::KillAllSessionsByPattern => "killAllSessionsByPattern",
            Self::KillCursors => "killCursors",
            Self::KillOp => "killOp",
            Self::KillSessions => "killSessions",
            Self::ListCollections => "listCollections",
            Self::ListCommands => "listCommands",
            Self::ListDatabases => "listDatabases",
            Self::ListIndexes => "listIndexes",
            Self::ListShards => "listShards",
            Self::LockInfo => "lockInfo",
            Self::Logout => "logout",
            Self::LogRotate => "logRotate",
            Self::MapReduce => "mapReduce",
            Self::Mapreduceshardedfinish => "mapreduce.shardedfinish",
            Self::MergeAuthzCollections => "_mergeAuthzCollections",
            Self::MergeChunks => "mergeChunks",
            Self::MigrateClone => "_migrateClone",
            Self::MoveChunk => "moveChunk",
            Self::MoveCollection => "moveCollection",
            Self::MovePrimary => "_movePrimary",
            Self::ParallelCollectionScan => "parallelCollectionScan",
            Self::Ping => "ping",
            Self::PlanCacheClear => "planCacheClear",
            Self::PlanCacheClearFilters => "planCacheClearFilters",
            Self::PlanCacheListFilters => "planCacheListFilters",
            Self::PlanCacheListPlans => "planCacheListPlans",
            Self::PlanCacheListQueryShapes => "planCacheListQueryShapes",
            Self::PlanCacheSetFilter => "planCacheSetFilter",
            Self::PrepareTransaction => "prepareTransaction",
            Self::Profile => "profile",
            Self::RecvChunkAbort => "_recvChunkAbort",
            Self::RecvChunkCommit => "_recvChunkCommit",
            Self::RecvChunkStart => "_recvChunkStart",
            Self::RecvChunkStatus => "_recvChunkStatus",
            Self::RefreshSessions => "refreshSessions",
            Self::RefreshSessionsInternal => "refreshSessionsInternal",
            Self::ReIndex => "reIndex",
            Self::RenameCollection => "renameCollection",
            Self::RepairCursor => "repairCursor",
            Self::RepairDatabase => "repairDatabase",
            Self::ReplSetAbortPrimaryCatchUp => "replSetAbortPrimaryCatchUp",
            Self::ReplSetDeclareElectionWinner => "replSetDeclareElectionWinner",
            Self::ReplSetElect => "replSetElect",
            Self::ReplSetFreeze => "replSetFreeze",
            Self::ReplSetFresh => "replSetFresh",
            Self::ReplSetGetConfig => "replSetGetConfig",
            Self::ReplSetGetRBID => "replSetGetRBID",
            Self::ReplSetGetStatus => "replSetGetStatus",
            Self::ReplSetHeartbeat => "replSetHeartbeat",
            Self::ReplSetInitiate => "replSetInitiate",
            Self::ReplSetMaintenance => "replSetMaintenance",
            Self::ReplSetReconfig => "replSetReconfig",
            Self::ReplSetRequestVotes => "replSetRequestVotes",
            Self::ReplSetResizeOplog => "replSetResizeOplog",
            Self::ReplSetStepDown => "replSetStepDown",
            Self::ReplSetStepUp => "replSetStepUp",
            Self::ReplSetSyncFrom => "replSetSyncFrom",
            Self::ReplSetUpdatePosition => "replSetUpdatePosition",
            Self::ResetError => "resetError",
            Self::ReshardCollection => "reshardCollection",
            Self::ReSync => "reSync",
            Self::RevokePrivilegesFromRole => "revokePrivilegesFromRole",
            Self::RevokeRolesFromRole => "revokeRolesFromRole",
            Self::RevokeRolesFromUser => "revokeRolesFromUser",
            Self::RolesInfo => "rolesInfo",
            Self::SaslContinue => "saslContinue",
            Self::SaslStart => "saslStart",
            Self::ServerStatus => "serverStatus",
            Self::SetFeatureCompatibilityVersion => "setFeatureCompatibilityVersion",
            Self::SetFreeMonitoring => "setFreeMonitoring",
            Self::SetParameter => "setParameter",
            Self::SetShardVersion => "setShardVersion",
            Self::ShardCollection => "shardCollection",
            Self::ShardConnPoolStats => "shardConnPoolStats",
            Self::ShardingState => "shardingState",
            Self::ShardsvrShardCollection => "_shardsvrShardCollection",
            Self::Shutdown => "shutdown",
            Self::SplitChunk => "splitChunk",
            Self::SplitVector => "splitVector",
            Self::StartSession => "startSession",
            Self::Top => "top",
            Self::Touch => "touch",
            Self::TransferMods => "_transferMods",
            Self::UnsetSharding => "unsetSharding",
            Self::UnshardCollection => "unshardCollection",
            Self::Update => "update",
            Self::UpdateRole => "updateRole",
            Self::UpdateUser => "updateUser",
            Self::UsersInfo => "usersInfo",
            Self::Validate => "validate",
            Self::WhatsMyUri => "whatsmyuri",
            Self::Writebacklisten => "writebacklisten",
        }
    }

    /// Returns `true` if this command is unconditionally blocked inside
    /// a multi-document transaction.
    #[must_use]
    pub const fn is_blocked_in_transaction(self) -> bool {
        matches!(
            self,
            Self::ReIndex
                | Self::CreateIndex
                | Self::CreateIndexes
                | Self::DropIndexes
                | Self::RenameCollection
                | Self::ListCollections
                | Self::Drop
                | Self::CurrentOp
                | Self::KillOp
        )
    }
}

impl FromStr for RequestType {
    type Err = DocumentDBError;

    #[expect(
        clippy::too_many_lines,
        reason = "This enum has a lot of variants, so this too_many_lines is expected since we capture all of them"
    )]
    fn from_str(cmd_name: &str) -> Result<Self> {
        match cmd_name {
            "abortTransaction" => Ok(Self::AbortTransaction),
            "aggregate" => Ok(Self::Aggregate),
            "appendOplogNote" => Ok(Self::AppendOplogNote),
            "applyOps" => Ok(Self::ApplyOps),
            "authenticate" => Ok(Self::Authenticate),
            "authSchemaUpgrade" => Ok(Self::AuthSchemaUpgrade),
            "availableQueryOptions" => Ok(Self::AvailableQueryOptions),
            "balancerStart" => Ok(Self::BalancerStart),
            "balancerStatus" => Ok(Self::BalancerStatus),
            "balancerStop" => Ok(Self::BalancerStop),
            "buildinfo" | "buildInfo" => Ok(Self::BuildInfo),
            "checkShardingIndex" => Ok(Self::CheckShardingIndex),
            "cleanupOrphaned" => Ok(Self::CleanupOrphaned),
            "clone" => Ok(Self::Clone),
            "_cloneCatalogData" => Ok(Self::CloneCatalogData),
            "cloneCollection" => Ok(Self::CloneCollection),
            "cloneCollectionAsCapped" => Ok(Self::CloneCollectionAsCapped),
            "_cloneCollectionOptionsFromPrimaryShard" => {
                Ok(Self::CloneCollectionOptionsFromPrimaryShard)
            }
            "collMod" => Ok(Self::CollMod),
            "collStats" => Ok(Self::CollStats),
            "commitTransaction" => Ok(Self::CommitTransaction),
            "compact" => Ok(Self::Compact),
            "_configsvrAddShard" => Ok(Self::ConfigsvrAddShard),
            "_configsvrAddShardToZone" => Ok(Self::ConfigsvrAddShardToZone),
            "_configsvrBalancerStart" => Ok(Self::ConfigsvrBalancerStart),
            "_configsvrBalancerStatus" => Ok(Self::ConfigsvrBalancerStatus),
            "_configsvrBalancerStop" => Ok(Self::ConfigsvrBalancerStop),
            "_configsvrCommitChunkMerge" => Ok(Self::ConfigsvrCommitChunkMerge),
            "_configsvrCommitChunkMigration" => Ok(Self::ConfigsvrCommitChunkMigration),
            "_configsvrCommitChunkSplit" => Ok(Self::ConfigsvrCommitChunkSplit),
            "_configsvrCommitMovePrimary" => Ok(Self::ConfigsvrCommitMovePrimary),
            "_configsvrCreateCollection" => Ok(Self::ConfigsvrCreateCollection),
            "_configsvrCreateDatabase" => Ok(Self::ConfigsvrCreateDatabase),
            "_configsvrDropCollection" => Ok(Self::ConfigsvrDropCollection),
            "_configsvrDropDatabase" => Ok(Self::ConfigsvrDropDatabase),
            "_configsvrEnableSharding" => Ok(Self::ConfigsvrEnableSharding),
            "_configsvrMoveChunk" => Ok(Self::ConfigsvrMoveChunk),
            "_configsvrMovePrimary" => Ok(Self::ConfigsvrMovePrimary),
            "_configsvrRemoveShard" => Ok(Self::ConfigsvrRemoveShard),
            "_configsvrRemoveShardFromZone" => Ok(Self::ConfigsvrRemoveShardFromZone),
            "_configsvrShardCollection" => Ok(Self::ConfigsvrShardCollection),
            "_configsvrUpdateZoneKeyRange" => Ok(Self::ConfigsvrUpdateZoneKeyRange),
            "connectionStatus" => Ok(Self::ConnectionStatus),
            "connPoolStats" => Ok(Self::ConnPoolStats),
            "connPoolSync" => Ok(Self::ConnPoolSync),
            "convertToCapped" => Ok(Self::ConvertToCapped),
            "copydb" => Ok(Self::Copydb),
            "copydbgetnonce" => Ok(Self::Copydbgetnonce),
            "copydbsaslstart" => Ok(Self::Copydbsaslstart),
            "count" => Ok(Self::Count),
            "create" => Ok(Self::Create),
            "createIndex" => Ok(Self::CreateIndex),
            "createIndexes" => Ok(Self::CreateIndexes),
            "createRole" => Ok(Self::CreateRole),
            "createUniqueOrCompoundIndexOnNestedPath" => {
                Ok(Self::CreateUniqueOrCompoundIndexOnNestedPath)
            }
            "createUser" => Ok(Self::CreateUser),
            "currentOp" => Ok(Self::CurrentOp),
            "currentOpCtx" => Ok(Self::CurrentOpCtx),
            "customAction" => Ok(Self::CustomAction),
            "dataSize" => Ok(Self::DataSize),
            "dbhash" | "dbHash" => Ok(Self::DbHash),
            "dbstats" | "dbStats" => Ok(Self::DbStats),
            "delete" => Ok(Self::Delete),
            "deleteIndexes" => Ok(Self::DeleteIndexes),
            "diagLogging" => Ok(Self::DiagLogging),
            "distinct" => Ok(Self::Distinct),
            "driverOIDTest" => Ok(Self::DriverOIDTest),
            "drop" => Ok(Self::Drop),
            "dropAllRolesFromDatabase" => Ok(Self::DropAllRolesFromDatabase),
            "dropAllUsersFromDatabase" => Ok(Self::DropAllUsersFromDatabase),
            "dropDatabase" => Ok(Self::DropDatabase),
            "dropIndexes" => Ok(Self::DropIndexes),
            "dropRole" => Ok(Self::DropRole),
            "dropUser" => Ok(Self::DropUser),
            "enableSharding" => Ok(Self::EnableSharding),
            "endSessions" => Ok(Self::EndSessions),
            "eval" => Ok(Self::Eval),
            "explain" => Ok(Self::Explain),
            "features" => Ok(Self::Features),
            "filemd5" => Ok(Self::Filemd5),
            "find" => Ok(Self::Find),
            "findandmodify" | "findAndModify" => Ok(Self::FindAndModify),
            "_flushDatabaseCacheUpdates" => Ok(Self::FlushDatabaseCacheUpdates),
            "_flushRoutingTableCacheUpdates" => Ok(Self::FlushRoutingTableCacheUpdates),
            "forceerror" => Ok(Self::Forceerror),
            "fsync" => Ok(Self::Fsync),
            "fsyncUnlock" => Ok(Self::FsyncUnlock),
            "geoNear" => Ok(Self::GeoNear),
            "geoSearch" => Ok(Self::GeoSearch),
            "getCmdLineOpts" => Ok(Self::GetCmdLineOpts),
            "getDatabaseVersion" => Ok(Self::GetDatabaseVersion),
            "getDefaultRWConcern" => Ok(Self::GetDefaultRWConcern),
            "getDiagnosticData" => Ok(Self::GetDiagnosticData),
            "getFreeMonitoringStatus" => Ok(Self::GetFreeMonitoringStatus),
            "getLastError" => Ok(Self::GetLastError),
            "getLastRequestStatistics" => Ok(Self::GetLastRequestStatistics),
            "getLog" => Ok(Self::GetLog),
            "getMore" => Ok(Self::GetMore),
            "_getNextSessionMods" => Ok(Self::GetNextSessionMods),
            "getNonce" => Ok(Self::GetNonce),
            "getParameter" => Ok(Self::GetParameter),
            "getPrevError" => Ok(Self::GetPrevError),
            "getShardMap" => Ok(Self::GetShardMap),
            "getShardVersion" => Ok(Self::GetShardVersion),
            "_getUserCacheGeneration" => Ok(Self::GetUserCacheGeneration),
            "grantPrivilegesToRole" => Ok(Self::GrantPrivilegesToRole),
            "grantRolesToRole" => Ok(Self::GrantRolesToRole),
            "grantRolesToUser" => Ok(Self::GrantRolesToUser),
            "group" => Ok(Self::Group),
            "handshake" => Ok(Self::Handshake),
            "hello" => Ok(Self::Hello),
            "hostInfo" => Ok(Self::HostInfo),
            "insert" => Ok(Self::Insert),
            "invalidateUserCache" => Ok(Self::InvalidateUserCache),
            "isdbgrid" => Ok(Self::Isdbgrid),
            "ismaster" | "isMaster" => Ok(Self::IsMaster),
            "_isSelf" => Ok(Self::IsSelf),
            "killAllSessions" => Ok(Self::KillAllSessions),
            "killAllSessionsByPattern" => Ok(Self::KillAllSessionsByPattern),
            "killCursors" => Ok(Self::KillCursors),
            "killOp" => Ok(Self::KillOp),
            "killSessions" => Ok(Self::KillSessions),
            "listCollections" => Ok(Self::ListCollections),
            "listCommands" => Ok(Self::ListCommands),
            "listDatabases" => Ok(Self::ListDatabases),
            "listIndexes" => Ok(Self::ListIndexes),
            "listShards" => Ok(Self::ListShards),
            "lockInfo" => Ok(Self::LockInfo),
            "logout" => Ok(Self::Logout),
            "logRotate" => Ok(Self::LogRotate),
            "mapReduce" => Ok(Self::MapReduce),
            "mapreduce.shardedfinish" => Ok(Self::Mapreduceshardedfinish),
            "_mergeAuthzCollections" => Ok(Self::MergeAuthzCollections),
            "mergeChunks" => Ok(Self::MergeChunks),
            "_migrateClone" => Ok(Self::MigrateClone),
            "moveChunk" => Ok(Self::MoveChunk),
            "moveCollection" => Ok(Self::MoveCollection),
            "_movePrimary" => Ok(Self::MovePrimary),
            "parallelCollectionScan" => Ok(Self::ParallelCollectionScan),
            "ping" => Ok(Self::Ping),
            "planCacheClear" => Ok(Self::PlanCacheClear),
            "planCacheClearFilters" => Ok(Self::PlanCacheClearFilters),
            "planCacheListFilters" => Ok(Self::PlanCacheListFilters),
            "planCacheListPlans" => Ok(Self::PlanCacheListPlans),
            "planCacheListQueryShapes" => Ok(Self::PlanCacheListQueryShapes),
            "planCacheSetFilter" => Ok(Self::PlanCacheSetFilter),
            "prepareTransaction" => Ok(Self::PrepareTransaction),
            "profile" => Ok(Self::Profile),
            "_recvChunkAbort" => Ok(Self::RecvChunkAbort),
            "_recvChunkCommit" => Ok(Self::RecvChunkCommit),
            "_recvChunkStart" => Ok(Self::RecvChunkStart),
            "_recvChunkStatus" => Ok(Self::RecvChunkStatus),
            "refreshSessions" => Ok(Self::RefreshSessions),
            "refreshSessionsInternal" => Ok(Self::RefreshSessionsInternal),
            "reindex" | "reIndex" => Ok(Self::ReIndex),
            "renameCollection" => Ok(Self::RenameCollection),
            "repairCursor" => Ok(Self::RepairCursor),
            "repairDatabase" => Ok(Self::RepairDatabase),
            "replSetAbortPrimaryCatchUp" => Ok(Self::ReplSetAbortPrimaryCatchUp),
            "replSetDeclareElectionWinner" => Ok(Self::ReplSetDeclareElectionWinner),
            "replSetElect" => Ok(Self::ReplSetElect),
            "replSetFreeze" => Ok(Self::ReplSetFreeze),
            "replSetFresh" => Ok(Self::ReplSetFresh),
            "replSetGetConfig" => Ok(Self::ReplSetGetConfig),
            "replSetGetRBID" => Ok(Self::ReplSetGetRBID),
            "replSetGetStatus" => Ok(Self::ReplSetGetStatus),
            "replSetHeartbeat" => Ok(Self::ReplSetHeartbeat),
            "replSetInitiate" => Ok(Self::ReplSetInitiate),
            "replSetMaintenance" => Ok(Self::ReplSetMaintenance),
            "replSetReconfig" => Ok(Self::ReplSetReconfig),
            "replSetRequestVotes" => Ok(Self::ReplSetRequestVotes),
            "replSetResizeOplog" => Ok(Self::ReplSetResizeOplog),
            "replSetStepDown" => Ok(Self::ReplSetStepDown),
            "replSetStepUp" => Ok(Self::ReplSetStepUp),
            "replSetSyncFrom" => Ok(Self::ReplSetSyncFrom),
            "replSetUpdatePosition" => Ok(Self::ReplSetUpdatePosition),
            "resetError" => Ok(Self::ResetError),
            "reshardCollection" => Ok(Self::ReshardCollection),
            "reSync" => Ok(Self::ReSync),
            "revokePrivilegesFromRole" => Ok(Self::RevokePrivilegesFromRole),
            "revokeRolesFromRole" => Ok(Self::RevokeRolesFromRole),
            "revokeRolesFromUser" => Ok(Self::RevokeRolesFromUser),
            "rolesInfo" => Ok(Self::RolesInfo),
            "saslContinue" => Ok(Self::SaslContinue),
            "saslStart" => Ok(Self::SaslStart),
            "serverStatus" => Ok(Self::ServerStatus),
            "setFeatureCompatibilityVersion" => Ok(Self::SetFeatureCompatibilityVersion),
            "setFreeMonitoring" => Ok(Self::SetFreeMonitoring),
            "setParameter" => Ok(Self::SetParameter),
            "setShardVersion" => Ok(Self::SetShardVersion),
            "shardCollection" => Ok(Self::ShardCollection),
            "shardConnPoolStats" => Ok(Self::ShardConnPoolStats),
            "shardingState" => Ok(Self::ShardingState),
            "_shardsvrShardCollection" => Ok(Self::ShardsvrShardCollection),
            "shutdown" => Ok(Self::Shutdown),
            "splitChunk" => Ok(Self::SplitChunk),
            "splitVector" => Ok(Self::SplitVector),
            "startSession" => Ok(Self::StartSession),
            "top" => Ok(Self::Top),
            "touch" => Ok(Self::Touch),
            "_transferMods" => Ok(Self::TransferMods),
            "unsetSharding" => Ok(Self::UnsetSharding),
            "unshardCollection" => Ok(Self::UnshardCollection),
            "update" => Ok(Self::Update),
            "updateRole" => Ok(Self::UpdateRole),
            "updateUser" => Ok(Self::UpdateUser),
            "usersInfo" => Ok(Self::UsersInfo),
            "validate" => Ok(Self::Validate),
            "whatsmyuri" => Ok(Self::WhatsMyUri),
            "writebacklisten" => Ok(Self::Writebacklisten),
            _ => Err(DocumentDBError::documentdb_error(
                ErrorCode::CommandNotFound,
                format!("Command '{cmd_name}' not found."),
            )),
        }
    }
}

impl fmt::Display for RequestType {
    #[expect(
        clippy::use_debug,
        reason = "Display delegates to Debug for enum variant names"
    )]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{self:?}")
    }
}
