{-# LANGUAGE TemplateHaskell #-}

module Hasura.Eventing.ScheduledTrigger.Types
  ( CronTriggerStats (CronTriggerStats, _ctsMaxScheduledTime, _ctsName),
    RetryContext (RetryContext, _rctxConf),
    ScheduledEventOp (..),
    ScheduledEventWebhookPayload (ScheduledEventWebhookPayload, sewpName, sewpScheduledTime, sewpRequestTransform, sewpResponseTransform),
    ScheduledTriggerInternalErr (ScheduledTriggerInternalErr),
  )
where

import Data.Aeson qualified as J
import Data.Aeson.TH qualified as J
import Data.Time.Clock
import Hasura.Base.Error
import Hasura.Eventing.HTTP
import Hasura.Logging qualified as L
import Hasura.Prelude
import Hasura.RQL.DDL.Webhook.Transform (MetadataResponseTransform, RequestTransform)
import Hasura.RQL.Types.EventTrigger
import Hasura.RQL.Types.ScheduledTrigger

newtype ScheduledTriggerInternalErr
  = ScheduledTriggerInternalErr QErr
  deriving (ScheduledTriggerInternalErr -> ScheduledTriggerInternalErr -> Bool
(ScheduledTriggerInternalErr
 -> ScheduledTriggerInternalErr -> Bool)
-> (ScheduledTriggerInternalErr
    -> ScheduledTriggerInternalErr -> Bool)
-> Eq ScheduledTriggerInternalErr
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ScheduledTriggerInternalErr -> ScheduledTriggerInternalErr -> Bool
$c/= :: ScheduledTriggerInternalErr -> ScheduledTriggerInternalErr -> Bool
== :: ScheduledTriggerInternalErr -> ScheduledTriggerInternalErr -> Bool
$c== :: ScheduledTriggerInternalErr -> ScheduledTriggerInternalErr -> Bool
Eq)

instance L.ToEngineLog ScheduledTriggerInternalErr L.Hasura where
  toEngineLog :: ScheduledTriggerInternalErr
-> (LogLevel, EngineLogType Hasura, Value)
toEngineLog (ScheduledTriggerInternalErr QErr
qerr) =
    (LogLevel
L.LevelError, EngineLogType Hasura
L.scheduledTriggerLogType, QErr -> Value
forall a. ToJSON a => a -> Value
J.toJSON QErr
qerr)

data CronTriggerStats = CronTriggerStats
  { CronTriggerStats -> TriggerName
_ctsName :: !TriggerName,
    CronTriggerStats -> Int
_ctsUpcomingEventsCount :: !Int,
    CronTriggerStats -> UTCTime
_ctsMaxScheduledTime :: !UTCTime
  }
  deriving (CronTriggerStats -> CronTriggerStats -> Bool
(CronTriggerStats -> CronTriggerStats -> Bool)
-> (CronTriggerStats -> CronTriggerStats -> Bool)
-> Eq CronTriggerStats
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CronTriggerStats -> CronTriggerStats -> Bool
$c/= :: CronTriggerStats -> CronTriggerStats -> Bool
== :: CronTriggerStats -> CronTriggerStats -> Bool
$c== :: CronTriggerStats -> CronTriggerStats -> Bool
Eq)

data RetryContext = RetryContext
  { RetryContext -> Int
_rctxTries :: !Int,
    RetryContext -> STRetryConf
_rctxConf :: !STRetryConf
  }
  deriving (RetryContext -> RetryContext -> Bool
(RetryContext -> RetryContext -> Bool)
-> (RetryContext -> RetryContext -> Bool) -> Eq RetryContext
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: RetryContext -> RetryContext -> Bool
$c/= :: RetryContext -> RetryContext -> Bool
== :: RetryContext -> RetryContext -> Bool
$c== :: RetryContext -> RetryContext -> Bool
Eq)

data ScheduledEventWebhookPayload = ScheduledEventWebhookPayload
  { ScheduledEventWebhookPayload -> EventId
sewpId :: !EventId,
    ScheduledEventWebhookPayload -> Maybe TriggerName
sewpName :: !(Maybe TriggerName),
    ScheduledEventWebhookPayload -> UTCTime
sewpScheduledTime :: !UTCTime,
    ScheduledEventWebhookPayload -> Value
sewpPayload :: !J.Value,
    ScheduledEventWebhookPayload -> Maybe Text
sewpComment :: !(Maybe Text),
    -- | sewpCreatedAt is the time at which the event was created,
    -- In case of one-off scheduled events, it's the time at which
    -- the user created the event and in case of cron triggers, the
    -- graphql-engine generator, generates the cron events, the
    -- `created_at` is just an implementation detail, so we
    -- don't send it
    ScheduledEventWebhookPayload -> Maybe UTCTime
sewpCreatedAt :: !(Maybe UTCTime),
    ScheduledEventWebhookPayload -> Maybe RequestTransform
sewpRequestTransform :: !(Maybe RequestTransform),
    ScheduledEventWebhookPayload -> Maybe MetadataResponseTransform
sewpResponseTransform :: !(Maybe MetadataResponseTransform)
  }
  deriving (Int -> ScheduledEventWebhookPayload -> ShowS
[ScheduledEventWebhookPayload] -> ShowS
ScheduledEventWebhookPayload -> String
(Int -> ScheduledEventWebhookPayload -> ShowS)
-> (ScheduledEventWebhookPayload -> String)
-> ([ScheduledEventWebhookPayload] -> ShowS)
-> Show ScheduledEventWebhookPayload
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ScheduledEventWebhookPayload] -> ShowS
$cshowList :: [ScheduledEventWebhookPayload] -> ShowS
show :: ScheduledEventWebhookPayload -> String
$cshow :: ScheduledEventWebhookPayload -> String
showsPrec :: Int -> ScheduledEventWebhookPayload -> ShowS
$cshowsPrec :: Int -> ScheduledEventWebhookPayload -> ShowS
Show, ScheduledEventWebhookPayload
-> ScheduledEventWebhookPayload -> Bool
(ScheduledEventWebhookPayload
 -> ScheduledEventWebhookPayload -> Bool)
-> (ScheduledEventWebhookPayload
    -> ScheduledEventWebhookPayload -> Bool)
-> Eq ScheduledEventWebhookPayload
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ScheduledEventWebhookPayload
-> ScheduledEventWebhookPayload -> Bool
$c/= :: ScheduledEventWebhookPayload
-> ScheduledEventWebhookPayload -> Bool
== :: ScheduledEventWebhookPayload
-> ScheduledEventWebhookPayload -> Bool
$c== :: ScheduledEventWebhookPayload
-> ScheduledEventWebhookPayload -> Bool
Eq)

$(J.deriveToJSON hasuraJSON {J.omitNothingFields = True} ''ScheduledEventWebhookPayload)

data ScheduledEventOp
  = SEOpRetry !UTCTime
  | SEOpStatus !ScheduledEventStatus
  deriving (Int -> ScheduledEventOp -> ShowS
[ScheduledEventOp] -> ShowS
ScheduledEventOp -> String
(Int -> ScheduledEventOp -> ShowS)
-> (ScheduledEventOp -> String)
-> ([ScheduledEventOp] -> ShowS)
-> Show ScheduledEventOp
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ScheduledEventOp] -> ShowS
$cshowList :: [ScheduledEventOp] -> ShowS
show :: ScheduledEventOp -> String
$cshow :: ScheduledEventOp -> String
showsPrec :: Int -> ScheduledEventOp -> ShowS
$cshowsPrec :: Int -> ScheduledEventOp -> ShowS
Show, ScheduledEventOp -> ScheduledEventOp -> Bool
(ScheduledEventOp -> ScheduledEventOp -> Bool)
-> (ScheduledEventOp -> ScheduledEventOp -> Bool)
-> Eq ScheduledEventOp
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ScheduledEventOp -> ScheduledEventOp -> Bool
$c/= :: ScheduledEventOp -> ScheduledEventOp -> Bool
== :: ScheduledEventOp -> ScheduledEventOp -> Bool
$c== :: ScheduledEventOp -> ScheduledEventOp -> Bool
Eq)