{-# OPTIONS_GHC -Wno-orphans #-}

-- | OpenApi implementation of 'WG.Cookie' and 'WG.SetCookie' traits.
module WebGear.OpenApi.Trait.Cookie () where

import Data.OpenApi hiding (Response)
import Data.Proxy (Proxy (Proxy))
import Data.String (fromString)
import Data.Text (Text)
import GHC.TypeLits (KnownSymbol, symbolVal)
import WebGear.Core.Request (Request)
import WebGear.Core.Response (Response)
import WebGear.Core.Trait (Get (..), Set (..), Trait, TraitAbsence)
import qualified WebGear.Core.Trait.Cookie as WG
import WebGear.OpenApi.Handler (DocNode (..), OpenApiHandler (..), nullNode, singletonNode)

instance (KnownSymbol name, TraitAbsence (WG.Cookie e name val) Request) => Get (OpenApiHandler m) (WG.Cookie e name val) Request where
  {-# INLINE getTrait #-}
  getTrait :: forall (ts :: [*]).
Prerequisite (Cookie e name val) ts Request =>
Cookie e name val
-> OpenApiHandler
     m
     (With Request ts)
     (Either
        (Absence (Cookie e name val) Request)
        (Attribute (Cookie e name val) Request))
getTrait Cookie e name val
WG.Cookie =
    Tree DocNode
-> OpenApiHandler
     m
     (With Request ts)
     (Either
        (Absence (Cookie e name val) Request)
        (Attribute (Cookie e name val) Request))
forall {k} {k1} {k2} (m :: k) (a :: k1) (b :: k2).
Tree DocNode -> OpenApiHandler m a b
OpenApiHandler (Tree DocNode
 -> OpenApiHandler
      m
      (With Request ts)
      (Either
         (Absence (Cookie e name val) Request)
         (Attribute (Cookie e name val) Request)))
-> Tree DocNode
-> OpenApiHandler
     m
     (With Request ts)
     (Either
        (Absence (Cookie e name val) Request)
        (Attribute (Cookie e name val) Request))
forall a b. (a -> b) -> a -> b
$ DocNode -> Tree DocNode
forall a. a -> Tree a
singletonNode (DocNode -> Tree DocNode) -> DocNode -> Tree DocNode
forall a b. (a -> b) -> a -> b
$ Text -> SecurityScheme -> DocNode
DocSecurityScheme Text
cookieName SecurityScheme
securityScheme
    where
      cookieName :: Text
cookieName = forall a. IsString a => String -> a
fromString @Text (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ Proxy name -> String
forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal (Proxy name -> String) -> Proxy name -> String
forall a b. (a -> b) -> a -> b
$ forall {k} (t :: k). Proxy t
forall (t :: Symbol). Proxy t
Proxy @name

      securityScheme :: SecurityScheme
      securityScheme :: SecurityScheme
securityScheme =
        SecurityScheme
          { _securitySchemeType :: SecuritySchemeType
_securitySchemeType =
              ApiKeyParams -> SecuritySchemeType
SecuritySchemeApiKey
                ApiKeyParams
                  { _apiKeyName :: Text
_apiKeyName = Text
cookieName
                  , _apiKeyIn :: ApiKeyLocation
_apiKeyIn = ApiKeyLocation
ApiKeyCookie
                  }
          , _securitySchemeDescription :: Maybe Text
_securitySchemeDescription = Maybe Text
forall a. Maybe a
Nothing
          }

-- Response cookie information is not captured by OpenAPI

instance (Trait (WG.SetCookie e name) Response) => Set (OpenApiHandler m) (WG.SetCookie e name) Response where
  {-# INLINE setTrait #-}
  setTrait :: forall (ts :: [*]).
SetCookie e name
-> (With Response ts
    -> Response
    -> Attribute (SetCookie e name) Response
    -> With Response (SetCookie e name : ts))
-> OpenApiHandler
     m
     (With Response ts, Attribute (SetCookie e name) Response)
     (With Response (SetCookie e name : ts))
setTrait SetCookie e name
WG.SetCookie With Response ts
-> Response
-> Attribute (SetCookie e name) Response
-> With Response (SetCookie e name : ts)
_ = Tree DocNode
-> OpenApiHandler
     m
     (With Response ts, Attribute (SetCookie e name) Response)
     (With Response (SetCookie e name : ts))
forall {k} {k1} {k2} (m :: k) (a :: k1) (b :: k2).
Tree DocNode -> OpenApiHandler m a b
OpenApiHandler Tree DocNode
forall a. Tree a
nullNode