{-# OPTIONS --safe #-}

open import Data.Nat.Properties using (+-0-monoid)
open import Data.Rational using ()

open import Ledger.Prelude
open import Ledger.Types.GovStructure

module Ledger.Enact (gs : _) (open GovStructure gs) where

open import Ledger.GovernanceActions gs


record EnactEnv : Type where
  field
    gid       : GovActionID
    treasury  : Coin
    epoch     : Epoch

record EnactState : Type where
  field
    cc            : HashProtected (Maybe ((Credential  Epoch) × ))
    constitution  : HashProtected (DocHash × Maybe ScriptHash)
    pv            : HashProtected ProtVer
    pparams       : HashProtected PParams
    withdrawals   : RwdAddr  Coin


instance
  unquoteDecl To-EnactEnv = derive-To
    [ (quote EnactEnv , To-EnactEnv) ]

open EnactState



ccCreds : HashProtected (Maybe ((Credential  Epoch) × ))   Credential
ccCreds (just x   , _)  = dom (x .proj₁)
ccCreds (nothing  , _)  = 

getHash :  {a}  NeedsHash a  Maybe GovActionID
getHash {NoConfidence}     h = just h
getHash {UpdateCommittee}  h = just h
getHash {NewConstitution}  h = just h
getHash {TriggerHF}        h = just h
getHash {ChangePParams}    h = just h
getHash {TreasuryWdrl}     _ = nothing
getHash {Info}             _ = nothing

getHashES : EnactState  GovActionType  Maybe GovActionID
getHashES es NoConfidence       = just (es .cc .proj₂)
getHashES es (UpdateCommittee)  = just (es .cc .proj₂)
getHashES es (NewConstitution)  = just (es .constitution .proj₂)
getHashES es (TriggerHF)        = just (es .pv .proj₂)
getHashES es (ChangePParams)    = just (es .pparams .proj₂)
getHashES es (TreasuryWdrl)     = nothing
getHashES es Info               = nothing


data


  _⊢_⇀⦇_,ENACT⦈_ : EnactEnv  EnactState  GovAction  EnactState  Type


open EnactState

private variable
  s : EnactState
  up : PParamsUpdate
  new : Credential  Epoch
  rem :  Credential
  q : 
  dh : DocHash
  sh : Maybe ScriptHash
  v : ProtVer
  wdrl : RwdAddr  Coin
  t : Coin
  gid : GovActionID
  e : Epoch

instance
  _ = +-0-monoid


open PParams using (ccMaxTermLength)
open EnactState using (cc)
data _⊢_⇀⦇_,ENACT⦈_ where


  Enact-NoConf :
    ───────────────────────────────────────
     gid , t , e   s ⇀⦇  NoConfidence , _ ⟧ᵍᵃ ,ENACT⦈ record s { cc = nothing , gid }

  Enact-UpdComm : let old      = maybe proj₁  (s .cc .proj₁)
                      maxTerm  = s .pparams .proj₁ .ccMaxTermLength +ᵉ e
                  in
    ∀[ term  range new ] term  maxTerm
    ───────────────────────────────────────
     gid , t , e   s ⇀⦇  UpdateCommittee , (new , rem , q) ⟧ᵍᵃ ,ENACT⦈
      record s { cc = just ((new ∪ˡ old)  rem  , q) , gid }

  Enact-NewConst :
    ───────────────────────────────────────
     gid , t , e   s ⇀⦇  NewConstitution , (dh , sh) ⟧ᵍᵃ ,ENACT⦈ record s { constitution = (dh , sh) , gid }


  Enact-HF :
    ───────────────────────────────────────
     gid , t , e   s ⇀⦇  TriggerHF , v ⟧ᵍᵃ ,ENACT⦈ record s { pv = v , gid }

  Enact-PParams :
    ───────────────────────────────────────
     gid , t , e   s ⇀⦇  ChangePParams , up ⟧ᵍᵃ ,ENACT⦈
      record s { pparams = applyUpdate (s .pparams .proj₁) up , gid }

  Enact-Wdrl : let newWdrls = s .withdrawals ∪⁺ wdrl in
    ∑[ x  newWdrls ] x  t
    ───────────────────────────────────────
     gid , t , e   s ⇀⦇  TreasuryWdrl , wdrl ⟧ᵍᵃ ,ENACT⦈ record s { withdrawals = newWdrls }

  Enact-Info :
    ───────────────────────────────────────
     gid , t , e   s ⇀⦇  Info , _ ⟧ᵍᵃ ,ENACT⦈ s