{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE UndecidableInstances #-}

-- | This module provides toml configuration for the custom single service.
module Navi.Services.Custom.Single.Toml
  ( SingleToml (..),
  )
where

import Navi.Data.NaviNote (NaviNote)
import Navi.Data.PollInterval (PollInterval (..), pollIntervalOptDecoder)
import Navi.Event.Toml
  ( ErrorNoteToml,
    RepeatEventToml,
    errorNoteOptDecoder,
    repeatEventOptDecoder,
  )
import Navi.Prelude
import Navi.Utils (commandDecoder)
import Pythia.Data.Command (Command (..))

-- | Codec for 'SingleToml'.
data SingleToml = MkSingleToml
  { -- | The command to run.
    SingleToml -> Command
command :: Command,
    -- | An optional name to be used with logging.
    SingleToml -> Maybe Text
name :: Maybe Text,
    -- | The alert trigger.
    SingleToml -> Text
triggerVal :: Text,
    -- | The poll interval.
    SingleToml -> Maybe PollInterval
pollInterval :: Maybe PollInterval,
    -- | The notification to send.
    SingleToml -> NaviNote
note :: NaviNote,
    -- | Determines how we treat repeat alerts.
    SingleToml -> Maybe RepeatEventToml
repeatEventCfg :: Maybe RepeatEventToml,
    -- | Determines how we handle errors.
    SingleToml -> Maybe ErrorNoteToml
errEventCfg :: Maybe ErrorNoteToml
  }
  deriving stock (SingleToml -> SingleToml -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SingleToml -> SingleToml -> Bool
$c/= :: SingleToml -> SingleToml -> Bool
== :: SingleToml -> SingleToml -> Bool
$c== :: SingleToml -> SingleToml -> Bool
Eq, Int -> SingleToml -> ShowS
[SingleToml] -> ShowS
SingleToml -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SingleToml] -> ShowS
$cshowList :: [SingleToml] -> ShowS
show :: SingleToml -> String
$cshow :: SingleToml -> String
showsPrec :: Int -> SingleToml -> ShowS
$cshowsPrec :: Int -> SingleToml -> ShowS
Show)

makeFieldLabelsNoPrefix ''SingleToml

-- | @since 0.1
instance DecodeTOML SingleToml where
  tomlDecoder :: Decoder SingleToml
tomlDecoder =
    Command
-> Maybe Text
-> Text
-> Maybe PollInterval
-> NaviNote
-> Maybe RepeatEventToml
-> Maybe ErrorNoteToml
-> SingleToml
MkSingleToml
      forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder Command
commandDecoder
      forall (f :: Type -> Type) a b.
Applicative f =>
f (a -> b) -> f a -> f b
<*> forall a. DecodeTOML a => Text -> Decoder (Maybe a)
getFieldOpt Text
"name"
      forall (f :: Type -> Type) a b.
Applicative f =>
f (a -> b) -> f a -> f b
<*> forall a. DecodeTOML a => Text -> Decoder a
getField Text
"trigger"
      forall (f :: Type -> Type) a b.
Applicative f =>
f (a -> b) -> f a -> f b
<*> Decoder (Maybe PollInterval)
pollIntervalOptDecoder
      forall (f :: Type -> Type) a b.
Applicative f =>
f (a -> b) -> f a -> f b
<*> forall a. DecodeTOML a => Text -> Decoder a
getField Text
"note"
      forall (f :: Type -> Type) a b.
Applicative f =>
f (a -> b) -> f a -> f b
<*> Decoder (Maybe RepeatEventToml)
repeatEventOptDecoder
      forall (f :: Type -> Type) a b.
Applicative f =>
f (a -> b) -> f a -> f b
<*> Decoder (Maybe ErrorNoteToml)
errorNoteOptDecoder