-- 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 API.BrigCommon where

import API.Common
import Data.Aeson.Types (Pair)
import Data.Maybe
import Testlib.Prelude as Prelude

data AddClient = AddClient
  { AddClient -> String
ctype :: String, -- "temporary", "permanent", "legalhold"
    AddClient -> Bool
internal :: Bool,
    AddClient -> String
clabel :: String,
    AddClient -> String
model :: String,
    AddClient -> Maybe [Value]
prekeys :: Maybe [Value],
    AddClient -> Maybe Value
lastPrekey :: Maybe Value,
    AddClient -> String
password :: String,
    AddClient -> Maybe [String]
acapabilities :: Maybe [String]
  }

instance Default AddClient where
  def :: AddClient
def =
    AddClient
      { ctype :: String
ctype = String
"permanent",
        internal :: Bool
internal = Bool
False,
        clabel :: String
clabel = String
"Test Device",
        model :: String
model = String
"Test Model",
        prekeys :: Maybe [Value]
prekeys = Maybe [Value]
forall a. Maybe a
Nothing,
        lastPrekey :: Maybe Value
lastPrekey = Maybe Value
forall a. Maybe a
Nothing,
        password :: String
password = String
defPassword,
        acapabilities :: Maybe [String]
acapabilities = [String] -> Maybe [String]
forall a. a -> Maybe a
Just [String
"legalhold-implicit-consent"]
      }

mkAddClientValue :: AddClient -> App [Pair]
mkAddClientValue :: AddClient -> App [Pair]
mkAddClientValue AddClient
args = do
  pks <- App [Value]
-> ([Value] -> App [Value]) -> Maybe [Value] -> App [Value]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ((Value -> [Value]) -> App Value -> App [Value]
forall a b. (a -> b) -> App a -> App b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Value -> [Value]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure App Value
getPrekey) [Value] -> App [Value]
forall a. a -> App a
forall (f :: * -> *) a. Applicative f => a -> f a
pure AddClient
args.prekeys
  lpk <- maybe getLastPrekey pure args.lastPrekey
  pure
    [ "prekeys" .= pks,
      "lastkey" .= lpk,
      "type" .= args.ctype,
      "label" .= args.clabel,
      "model" .= args.model,
      "password" .= args.password,
      "capabilities" .= args.acapabilities
    ]