module arch.node.net.node_proxy_behaviour;

import arch.node.net.node_proxy_messages open;
import arch.node.net.node_proxy_config open;
import arch.node.net.node_proxy_environment open;

import arch.node.types.basics open;
import arch.node.types.identities open;
import arch.node.types.messages open;
import arch.node.types.engine open;
import arch.node.types.anoma as Anoma open;

NodeProxyActionArguments : Type := Unit;

NodeProxyAction : Type :=
  Action
    NodeProxyLocalCfg
    NodeProxyLocalState
    NodeProxyMailboxState
    NodeProxyTimerHandle
    NodeProxyActionArguments
    Anoma.Msg
    Anoma.Cfg
    Anoma.Env;

NodeProxyActionInput : Type :=
  ActionInput
    NodeProxyLocalCfg
    NodeProxyLocalState
    NodeProxyMailboxState
    NodeProxyTimerHandle
    NodeProxyActionArguments
    Anoma.Msg;

NodeProxyActionEffect : Type :=
  ActionEffect
    NodeProxyLocalState
    NodeProxyMailboxState
    NodeProxyTimerHandle
    Anoma.Msg
    Anoma.Cfg
    Anoma.Env;

NodeProxyActionExec : Type :=
  ActionExec
    NodeProxyLocalCfg
    NodeProxyLocalState
    NodeProxyMailboxState
    NodeProxyTimerHandle
    NodeProxyActionArguments
    Anoma.Msg
    Anoma.Cfg
    Anoma.Env;

exampleReplyAction
  (input : NodeProxyActionInput) : Option NodeProxyActionEffect := TODO;

{-
let
cfg := ActionInput.cfg input;
env := ActionInput.env input;
trigger := ActionInput.trigger input;
args := ActionInput.args input;
in
case getEngineMsgFromTimestampedTrigger trigger of {
| some mkEngineMsg@{
msg := Anoma.MsgNodeProxy (NodeProxyMsgExampleRequest req);
sender := sender;
target := target;
mailbox := mailbox;
} :=
some mkActionEffect@{
env := env;
msgs := [
mkEngineMsg@{
sender := getEngineIDFromEngineCfg cfg;
target := sender;
mailbox := some 0;
msg :=
Anoma.MsgNodeProxy
(NodeProxyMsgExampleReply
(ok mkExampleReplyOk@{
argOne := ExampleRequest.argOne req;
}));
}
];
timers := [];
engines := [];
}
| _ := none
-}
exampleReplyActionLabel : NodeProxyActionExec := Seq [exampleReplyAction];

NodeProxyGuard : Type :=
  Guard
    NodeProxyLocalCfg
    NodeProxyLocalState
    NodeProxyMailboxState
    NodeProxyTimerHandle
    NodeProxyActionArguments
    Anoma.Msg
    Anoma.Cfg
    Anoma.Env;

NodeProxyGuardOutput : Type :=
  GuardOutput
    NodeProxyLocalCfg
    NodeProxyLocalState
    NodeProxyMailboxState
    NodeProxyTimerHandle
    NodeProxyActionArguments
    Anoma.Msg
    Anoma.Cfg
    Anoma.Env;

NodeProxyGuardEval : Type :=
  GuardEval
    NodeProxyLocalCfg
    NodeProxyLocalState
    NodeProxyMailboxState
    NodeProxyTimerHandle
    NodeProxyActionArguments
    Anoma.Msg
    Anoma.Cfg
    Anoma.Env;

exampleReplyGuard
  (trigger : NodeProxyTimestampedTrigger)
  (cfg : NodeProxyCfg)
  (env : NodeProxyEnv)
  : Option NodeProxyGuardOutput := TODO;

{-
case getEngineMsgFromTimestampedTrigger trigger of {
| some mkEngineMsg@{
msg := Anoma.MsgNodeProxy (NodeProxyMsgExampleRequest req);
sender := mkPair none _; -- from local engines only (NodeID is none)
} := some mkGuardOutput@{
action := exampleReplyActionLabel;
args := [];
}
| _ := none
-}
NodeProxyBehaviour : Type :=
  EngineBehaviour
    NodeProxyLocalCfg
    NodeProxyLocalState
    NodeProxyMailboxState
    NodeProxyTimerHandle
    NodeProxyActionArguments
    Anoma.Msg
    Anoma.Cfg
    Anoma.Env;

module node_proxy_behaviour_example;
  
  exNodeProxyBehaviour : NodeProxyBehaviour :=
    mkEngineBehaviour@{
      guards := First [exampleReplyGuard];
    };

end;