-- This file is part of the Wire Server implementation.
--
-- Copyright (C) 2025 Wire Swiss GmbH <opensource@wire.com>
--
-- This program is free software: you can redistribute it and/or modify it under
-- the terms of the GNU Affero General Public License as published by the Free
-- Software Foundation, either version 3 of the License, or (at your option) any
-- later version.
--
-- This program is distributed in the hope that it will be useful, but WITHOUT
-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-- FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
-- details.
--
-- You should have received a copy of the GNU Affero General Public License along
-- with this program. If not, see <https://www.gnu.org/licenses/>.

module Test.FeatureFlags.MlsMigration where

import qualified API.Galley as Public
import qualified API.GalleyInternal as Internal
import qualified Data.Aeson as A
import SetupHelpers
import Test.FeatureFlags.Util
import Testlib.Prelude

testMlsMigration :: (HasCallStack) => APIAccess -> App ()
testMlsMigration :: HasCallStack => APIAccess -> App ()
testMlsMigration APIAccess
access = do
  -- first we have to enable mls
  (owner, tid, _) <- Domain -> Int -> App (Value, String, [Value])
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> Int -> App (Value, String, [Value])
createTeam Domain
OwnDomain Int
0
  void $ Public.setTeamFeatureConfig owner tid "mls" mlsEnable >>= getJSON 200
  mkFeatureTests "mlsMigration"
    & addUpdate mlsMigrationConfig1
    & addUpdate mlsMigrationConfig2
    & setOwner owner
      >>= runFeatureTests OwnDomain access

testMlsMigrationDefaults :: (HasCallStack) => App ()
testMlsMigrationDefaults :: HasCallStack => App ()
testMlsMigrationDefaults = do
  ServiceOverrides -> (HasCallStack => String -> App ()) -> App ()
forall a.
HasCallStack =>
ServiceOverrides -> (HasCallStack => String -> App a) -> App a
withModifiedBackend
    ServiceOverrides
forall a. Default a => a
def
      { galleyCfg = setField "settings.featureFlags.mlsMigration.defaults.lockStatus" "unlocked"
      }
    ((HasCallStack => String -> App ()) -> App ())
-> (HasCallStack => String -> App ()) -> App ()
forall a b. (a -> b) -> a -> b
$ \String
domain -> do
      (owner, tid, _) <- String -> Int -> App (Value, String, [Value])
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> Int -> App (Value, String, [Value])
createTeam String
domain Int
0
      void
        $ Internal.patchTeamFeature owner tid "mls" (object ["status" .= "enabled"])
        >>= getJSON 200
      feat <- Internal.getTeamFeature owner tid "mlsMigration" >>= getJSON 200
      feat %. "config" `shouldMatch` mlsMigrationDefaultConfig

mlsEnableConfig :: Value
mlsEnableConfig :: Value
mlsEnableConfig =
  [Pair] -> Value
object
    [ String
"protocolToggleUsers" String -> [String] -> Pair
forall a. ToJSON a => String -> a -> Pair
.= ([] :: [String]),
      String
"defaultProtocol" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"mls",
      String
"supportedProtocols" String -> [String] -> Pair
forall a. ToJSON a => String -> a -> Pair
.= [String
"mls"],
      String
"allowedCipherSuites" String -> [Int] -> Pair
forall a. ToJSON a => String -> a -> Pair
.= ([Int
1] :: [Int]),
      String
"defaultCipherSuite" String -> Value -> Pair
forall a. ToJSON a => String -> a -> Pair
.= Scientific -> Value
A.Number Scientific
1
    ]

mlsEnable :: Value
mlsEnable :: Value
mlsEnable =
  [Pair] -> Value
object
    [ String
"status" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"enabled",
      String
"config" String -> Value -> Pair
forall a. ToJSON a => String -> a -> Pair
.= Value
mlsEnableConfig
    ]

mlsMigrationDefaultConfig :: Value
mlsMigrationDefaultConfig :: Value
mlsMigrationDefaultConfig =
  [Pair] -> Value
object
    [ String
"startTime" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"2029-05-16T10:11:12.123Z",
      String
"finaliseRegardlessAfter" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"2029-10-17T00:00:00Z"
    ]

mlsMigrationConfig1 :: Value
mlsMigrationConfig1 :: Value
mlsMigrationConfig1 =
  [Pair] -> Value
object
    [ String
"status" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"enabled",
      String
"config"
        String -> Value -> Pair
forall a. ToJSON a => String -> a -> Pair
.= [Pair] -> Value
object
          [ String
"startTime" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"2029-05-16T10:11:12.123Z",
            String
"finaliseRegardlessAfter" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"2030-10-17T00:00:00Z"
          ]
    ]

mlsMigrationConfig2 :: Value
mlsMigrationConfig2 :: Value
mlsMigrationConfig2 =
  [Pair] -> Value
object
    [ String
"status" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"enabled",
      String
"config"
        String -> Value -> Pair
forall a. ToJSON a => String -> a -> Pair
.= [Pair] -> Value
object
          [ String
"startTime" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"2030-05-16T10:11:12.123Z",
            String
"finaliseRegardlessAfter" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"2031-10-17T00:00:00Z"
          ]
    ]