-- This file is part of the Wire Server implementation.
--
-- Copyright (C) 2026 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.License where

import API.Galley
import SetupHelpers
import Test.Meetings (withLicensedBackend, withLicensedBackendFlags)
import Testlib.Prelude

assertMeetingsFlags :: (HasCallStack, MakesValue resp) => resp -> [(String, Bool)] -> App ()
assertMeetingsFlags :: forall resp.
(HasCallStack, MakesValue resp) =>
resp -> [(String, Bool)] -> App ()
assertMeetingsFlags resp
resp [(String, Bool)]
flags =
  [(String, Bool)] -> ((String, Bool) -> App ()) -> App ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(String, Bool)]
flags (((String, Bool) -> App ()) -> App ())
-> ((String, Bool) -> App ()) -> App ()
forall a b. (a -> b) -> a -> b
$ \(String
flag, Bool
expected) -> do
    v <- resp
resp resp -> String -> App Value
forall a. (HasCallStack, MakesValue a) => a -> String -> App Value
%. (String
"meetings." String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
flag)
    asBool v >>= (`shouldMatch` expected)

testLicense :: (HasCallStack) => App ()
testLicense :: HasCallStack => App ()
testLicense = do
  -- Unlicensed static backend: all flags false.
  (owner, _, _) <- Domain -> Int -> App (Value, String, [Value])
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> Int -> App (Value, String, [Value])
createTeam Domain
OwnDomain Int
1
  resp <- getLicense owner >>= getJSON 200
  assertMeetingsFlags
    resp
    [("invitation_links", False), ("emails_sending", False), ("calendars_integration", False)]
  -- Licensed dynamic backend: all flags true (meetings premium activated).
  withLicensedBackend $ \String
dom -> do
    (ownerL, _, _) <- String -> Int -> App (Value, String, [Value])
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> Int -> App (Value, String, [Value])
createTeam String
dom Int
1
    respL <- getLicense ownerL >>= getJSON 200
    assertMeetingsFlags
      respL
      [("invitation_links", True), ("emails_sending", True), ("calendars_integration", True)]

-- | Mixed licence: invitation links and calendars integration licensed while
-- email sending is off — each flag is served independently, exactly as
-- licensed (review feedback on WPB-29669).
testLicenseMixedFlags :: (HasCallStack) => App ()
testLicenseMixedFlags :: HasCallStack => App ()
testLicenseMixedFlags =
  Bool -> Bool -> Bool -> (String -> App ()) -> App ()
forall a.
HasCallStack =>
Bool -> Bool -> Bool -> (String -> App a) -> App a
withLicensedBackendFlags Bool
True Bool
False Bool
True ((String -> App ()) -> App ()) -> (String -> App ()) -> App ()
forall a b. (a -> b) -> a -> b
$ \String
dom -> do
    (owner, _, _) <- String -> Int -> App (Value, String, [Value])
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> Int -> App (Value, String, [Value])
createTeam String
dom Int
1
    resp <- getLicense owner >>= getJSON 200
    assertMeetingsFlags
      resp
      [("invitation_links", True), ("emails_sending", False), ("calendars_integration", True)]