-- 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.Nginz where

import API.Cargohold
import qualified Codec.MIME.Type as MIME
import qualified Data.Aeson as Aeson
import qualified Data.ByteString.Lazy as LBS
import qualified Network.HTTP.Client as HTTP
import Testlib.Prelude

getSystemSettingsUnAuthorized :: (HasCallStack, MakesValue domain) => domain -> App Response
getSystemSettingsUnAuthorized :: forall domain.
(HasCallStack, MakesValue domain) =>
domain -> App Response
getSystemSettingsUnAuthorized domain
domain = do
  req <- domain -> Service -> Versioned -> String -> App Request
forall user.
(HasCallStack, MakesValue user) =>
user -> Service -> Versioned -> String -> App Request
baseRequest domain
domain Service
Nginz Versioned
Versioned String
"/system/settings/unauthorized"
  submit "GET" req

login :: (HasCallStack, MakesValue domain, MakesValue email, MakesValue password) => domain -> email -> password -> App Response
login :: forall domain email password.
(HasCallStack, MakesValue domain, MakesValue email,
 MakesValue password) =>
domain -> email -> password -> App Response
login domain
domain email
email password
pw = do
  req <- domain -> Service -> Versioned -> String -> App Request
forall user.
(HasCallStack, MakesValue user) =>
user -> Service -> Versioned -> String -> App Request
rawBaseRequest domain
domain Service
Nginz Versioned
Unversioned String
"/login"
  emailStr <- make email >>= asString
  pwStr <- make pw >>= asString
  submit "POST" (req & addJSONObject ["email" .= emailStr, "password" .= pwStr, "label" .= "auth"])

loginWith2ndFactor :: (HasCallStack, MakesValue domain, MakesValue email, MakesValue password, MakesValue sndFactor) => domain -> email -> password -> sndFactor -> App Response
loginWith2ndFactor :: forall domain email password sndFactor.
(HasCallStack, MakesValue domain, MakesValue email,
 MakesValue password, MakesValue sndFactor) =>
domain -> email -> password -> sndFactor -> App Response
loginWith2ndFactor domain
domain email
email password
pw sndFactor
sf = do
  req <- domain -> Service -> Versioned -> String -> App Request
forall user.
(HasCallStack, MakesValue user) =>
user -> Service -> Versioned -> String -> App Request
rawBaseRequest domain
domain Service
Nginz Versioned
Unversioned String
"/login"
  emailStr <- make email >>= asString
  pwStr <- make pw >>= asString
  sfStr <- make sf >>= asString
  submit "POST" (req & addJSONObject ["email" .= emailStr, "password" .= pwStr, "label" .= "auth", "verification_code" .= sfStr])

access :: (HasCallStack, MakesValue domain, MakesValue cookie) => domain -> cookie -> App Response
access :: forall domain cookie.
(HasCallStack, MakesValue domain, MakesValue cookie) =>
domain -> cookie -> App Response
access domain
domain cookie
cookie = do
  req <- domain -> Service -> Versioned -> String -> App Request
forall user.
(HasCallStack, MakesValue user) =>
user -> Service -> Versioned -> String -> App Request
rawBaseRequest domain
domain Service
Nginz Versioned
Unversioned String
"/access"
  cookieStr <- make cookie >>= asString
  submit "POST" (req & setCookie cookieStr)

logout :: (HasCallStack, MakesValue domain, MakesValue cookie, MakesValue token) => domain -> cookie -> token -> App Response
logout :: forall domain email password.
(HasCallStack, MakesValue domain, MakesValue email,
 MakesValue password) =>
domain -> email -> password -> App Response
logout domain
d cookie
c token
t = do
  req <- domain -> Service -> Versioned -> String -> App Request
forall user.
(HasCallStack, MakesValue user) =>
user -> Service -> Versioned -> String -> App Request
rawBaseRequest domain
d Service
Nginz Versioned
Unversioned String
"/access/logout"
  cookie <- make c & asString
  token <- make t & asString
  submit "POST" (req & setCookie cookie & addHeader "Authorization" ("Bearer " <> token))

getConversation :: (HasCallStack, MakesValue user, MakesValue qcnv, MakesValue token) => user -> qcnv -> token -> App Response
getConversation :: forall domain email password.
(HasCallStack, MakesValue domain, MakesValue email,
 MakesValue password) =>
domain -> email -> password -> App Response
getConversation user
user qcnv
qcnv token
t = do
  (domain, cnv) <- qcnv -> App (String, String)
forall a. (HasCallStack, MakesValue a) => a -> App (String, String)
objQid qcnv
qcnv
  token <- make t & asString
  req <- rawBaseRequest user Nginz Versioned (joinHttpPath ["conversations", domain, cnv])
  submit "GET" (req & addHeader "Authorization" ("Bearer " <> token))

uploadProviderAsset :: (HasCallStack, MakesValue domain) => domain -> String -> String -> App Response
uploadProviderAsset :: forall domain.
(HasCallStack, MakesValue domain) =>
domain -> String -> String -> App Response
uploadProviderAsset domain
domain String
cookie String
payload = do
  req <- domain -> Service -> Versioned -> String -> App Request
forall user.
(HasCallStack, MakesValue user) =>
user -> Service -> Versioned -> String -> App Request
rawBaseRequest domain
domain Service
Nginz Versioned
Versioned (String -> App Request) -> String -> App Request
forall a b. (a -> b) -> a -> b
$ [String] -> String
joinHttpPath [String
"provider", String
"assets"]
  bdy <- txtAsset payload
  submit "POST"
    $ req
    & setCookie cookie
    & addBody bdy multipartMixedMime

buildUploadAssetRequestBody ::
  (HasCallStack, MakesValue assetRetention) =>
  Bool ->
  assetRetention ->
  LBS.ByteString ->
  MIME.MIMEType ->
  App HTTP.RequestBody
buildUploadAssetRequestBody :: forall assetRetention.
(HasCallStack, MakesValue assetRetention) =>
Bool -> assetRetention -> ByteString -> MIMEType -> App RequestBody
buildUploadAssetRequestBody Bool
isPublic assetRetention
retention ByteString
body MIMEType
mimeType = do
  mbRetention <- assetRetention -> App Value
forall a. (MakesValue a, HasCallStack) => a -> App Value
make assetRetention
retention
  let header' :: Aeson.Value
      header' =
        [Pair] -> Value
Aeson.object
          [ String
"public" String -> Bool -> Pair
forall a. ToJSON a => String -> a -> Pair
.= Bool
isPublic,
            String
"retention" String -> Value -> Pair
forall a. ToJSON a => String -> a -> Pair
.= Value
mbRetention
          ]
  HTTP.RequestBodyLBS <$> buildMultipartBody header' body mimeType

upgradePersonalToTeam :: (HasCallStack, MakesValue user) => user -> String -> String -> App Response
upgradePersonalToTeam :: forall domain.
(HasCallStack, MakesValue domain) =>
domain -> String -> String -> App Response
upgradePersonalToTeam user
user String
token String
name = do
  req <- user -> Service -> Versioned -> String -> App Request
forall user.
(HasCallStack, MakesValue user) =>
user -> Service -> Versioned -> String -> App Request
baseRequest user
user Service
Brig Versioned
Versioned (String -> App Request) -> String -> App Request
forall a b. (a -> b) -> a -> b
$ [String] -> String
joinHttpPath [String
"upgrade-personal-to-team"]
  submit "POST" $ req & addJSONObject ["name" .= name, "icon" .= "default"] & addHeader "Authorization" ("Bearer " <> token)