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

import SetupHelpers (createTeam)
import Test.FeatureFlags.Util
import Testlib.Prelude

testPatchMeetingPremium :: (HasCallStack) => App ()
testPatchMeetingPremium :: HasCallStack => App ()
testPatchMeetingPremium = Int -> App () -> App ()
forall a. Int -> App a -> App a
withAPIVersion Int
16 (App () -> App ()) -> App () -> App ()
forall a b. (a -> b) -> a -> b
$ Domain -> String -> Value -> App ()
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> String -> Value -> App ()
checkPatch Domain
OwnDomain String
"meetingsPremium" Value
disabledLocked

testMeetingPremium :: (HasCallStack) => APIAccess -> App ()
testMeetingPremium :: HasCallStack => APIAccess -> App ()
testMeetingPremium APIAccess
access =
  Int -> App () -> App ()
forall a. Int -> App a -> App a
withAPIVersion Int
16
    (App () -> App ()) -> App () -> App ()
forall a b. (a -> b) -> a -> b
$ String -> FeatureTests
mkFeatureTests String
"meetingsPremium"
    FeatureTests -> (FeatureTests -> FeatureTests) -> FeatureTests
forall a b. a -> (a -> b) -> b
& Value -> FeatureTests -> FeatureTests
addUpdate Value
enabled
    FeatureTests -> (FeatureTests -> App ()) -> App ()
forall a b. a -> (a -> b) -> b
& Domain -> APIAccess -> FeatureTests -> App ()
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> APIAccess -> FeatureTests -> App ()
runFeatureTests Domain
OwnDomain APIAccess
access

-- | WPB-26771: the public meetingsPremium endpoints are gated at v17 (404)
-- while remaining available through v16. Only the v16 GET is asserted here:
-- v16 PUT success is covered by 'testMeetingPremium' (whose runFeatureTests
-- unlocks the feature first), and a public PUT in this test would 409
-- feature-locked against the default enabled+locked state.
testMeetingPremiumRemovedAtV17 :: (HasCallStack) => App ()
testMeetingPremiumRemovedAtV17 :: HasCallStack => App ()
testMeetingPremiumRemovedAtV17 = do
  (owner, tid, _) <- Domain -> Int -> App (Value, String, [Value])
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> Int -> App (Value, String, [Value])
createTeam Domain
OwnDomain Int
0
  let p = [String] -> String
joinHttpPath [String
"teams", String
tid, String
"features", String
"meetingsPremium"]
      body = [Pair] -> Value
object [String
"status" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"enabled", String
"lockStatus" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"locked"]
  bindResponse (baseRequest owner Galley (ExplicitVersion 17) p >>= submit "GET") $ \Response
resp -> do
    Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
404
  bindResponse (baseRequest owner Galley (ExplicitVersion 17) p <&> addJSON body >>= submit "PUT") $ \Response
resp -> do
    Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
404
  bindResponse (baseRequest owner Galley (ExplicitVersion 16) p >>= submit "GET") $ \Response
resp -> do
    Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
200
    Response
resp.json Maybe Value -> String -> App Value
forall a. (HasCallStack, MakesValue a) => a -> String -> App Value
%. String
"status" App Value -> String -> App ()
forall a b.
(MakesValue a, MakesValue b, HasCallStack) =>
a -> b -> App ()
`shouldMatch` String
"enabled"
    Response
resp.json Maybe Value -> String -> App Value
forall a. (HasCallStack, MakesValue a) => a -> String -> App Value
%. String
"lockStatus" App Value -> String -> App ()
forall a b.
(MakesValue a, MakesValue b, HasCallStack) =>
a -> b -> App ()
`shouldMatch` String
"locked"

-- | WPB-26771: the aggregate list endpoints 'GET /feature-configs' and
-- 'GET /teams/:tid/features' are version-agnostic — like 'From'-gated features
-- (e.g. MLS), they include every feature at every API version. Even though the
-- dedicated meetingsPremium endpoints 404 at v17, both list endpoints keep
-- returning the (default enabled+locked) meetingsPremium entry. This test
-- locks that behaviour in.
testMeetingPremiumListedAtV17 :: (HasCallStack) => App ()
testMeetingPremiumListedAtV17 :: HasCallStack => App ()
testMeetingPremiumListedAtV17 = do
  (owner, tid, _) <- Domain -> Int -> App (Value, String, [Value])
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> Int -> App (Value, String, [Value])
createTeam Domain
OwnDomain Int
0
  let assertMeetingPremium r
resp = do
        r
resp.status a -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
200
        mp <- r
resp.json a -> String -> App Value
forall a. (HasCallStack, MakesValue a) => a -> String -> App Value
%. String
"meetingsPremium"
        mp %. "status" `shouldMatch` "enabled"
        mp %. "lockStatus" `shouldMatch` "locked"
      teamFeatures = [String] -> String
joinHttpPath [String
"teams", String
tid, String
"features"]
  bindResponse (baseRequest owner Galley (ExplicitVersion 17) "/feature-configs" >>= submit "GET") assertMeetingPremium
  bindResponse (baseRequest owner Galley (ExplicitVersion 17) teamFeatures >>= submit "GET") assertMeetingPremium