{-# OPTIONS --safe #-}

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

open import Ledger.Prelude
open import Ledger.Dijkstra.Specification.Gov.Base

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

open import Ledger.Dijkstra.Specification.Gov.Actions gs


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

-- EnactState is modified only by enacting governance action
-- All fields contain a GovActionID since they are hash-protected.
record EnactState : Type where
  field
    cc            : HashProtected (Maybe ((Credential  Epoch) × ))
    constitution  : HashProtected (DocHash × Maybe ScriptHash)
    pv            : HashProtected ProtVer
    pparams       : HashProtected PParams
    withdrawals   : Withdrawals


record HasEnactState {a} (A : Type a) : Type a where
  field EnactStateOf : A  EnactState
open HasEnactState ⦃...⦄ public

instance
  HasPParams-EnactState : HasPParams EnactState
  HasPParams-EnactState .PParamsOf = proj₁  EnactState.pparams

  HasccMaxTermLength-EnactState : HasCCMaxTermLength EnactState
  HasccMaxTermLength-EnactState .CCMaxTermLengthOf = PParams.ccMaxTermLength  PParamsOf

  HasWithdrawals-EnactState : HasWithdrawals EnactState
  HasWithdrawals-EnactState .WithdrawalsOf = EnactState.withdrawals

  unquoteDecl HasCast-EnactEnv = derive-HasCast
    [ (quote EnactEnv , HasCast-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 {TriggerHardFork}     h = just h
getHash {ChangePParams}       h = just h
getHash {TreasuryWithdrawal}  _ = 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 (TriggerHardFork)     = just (es .pv .proj₂)
getHashES es (ChangePParams)       = just (es .pparams .proj₂)
getHashES es (TreasuryWithdrawal)  = nothing
getHashES es Info                  = nothing


private variable
  s        : EnactState
  up       : PParamsUpdate
  new      : Credential  Epoch
  removed  :  Credential
  q        : 
  dHash    : DocHash
  sHash    : Maybe ScriptHash
  protVer  : ProtVer
  wdrls    : Withdrawals
  t        : Treasury
  gid      : GovActionID
  ep       : Epoch

instance
  _ = +-0-monoid


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

  Enact-NoConf :
    ───────────────────────────────────────
    $\begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Enact.html#2702}{\htmlId{2923}{\htmlClass{Generalizable}{\text{gid}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Enact.html#2680}{\htmlId{2929}{\htmlClass{Generalizable}{\text{t}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Enact.html#2727}{\htmlId{2933}{\htmlClass{Generalizable}{\text{ep}}}}\, \end{pmatrix}$  s ⇀⦇ $\begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Gov.Actions.html#571}{\htmlId{2947}{\htmlClass{InductiveConstructor}{\text{NoConfidence}}}}\, \\ \,\htmlId{2962}{\htmlClass{Symbol}{\text{\_}}}\, \end{pmatrix}$ ,ENACT⦈ record s { cc = nothing , gid }

  Enact-UpdComm : let old      = maybe proj₁  (s .cc .proj₁)
                      maxTerm  = CCMaxTermLengthOf s +ᵉ ep
                  in
    ∀[ term  range new ] term  maxTerm
    ───────────────────────────────────────
    $\begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Enact.html#2702}{\htmlId{3242}{\htmlClass{Generalizable}{\text{gid}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Enact.html#2680}{\htmlId{3248}{\htmlClass{Generalizable}{\text{t}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Enact.html#2727}{\htmlId{3252}{\htmlClass{Generalizable}{\text{ep}}}}\, \end{pmatrix}$  s ⇀⦇ $\begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Gov.Actions.html#609}{\htmlId{3266}{\htmlClass{InductiveConstructor}{\text{UpdateCommittee}}}}\, \\ \,\htmlId{3284}{\htmlClass{Symbol}{\text{(}}}\,\,\href{Ledger.Dijkstra.Specification.Enact.html#2510}{\htmlId{3285}{\htmlClass{Generalizable}{\text{new}}}}\, , \,\href{Ledger.Dijkstra.Specification.Enact.html#2542}{\htmlId{3291}{\htmlClass{Generalizable}{\text{removed}}}}\, , \,\href{Ledger.Dijkstra.Specification.Enact.html#2568}{\htmlId{3301}{\htmlClass{Generalizable}{\text{q}}}}\,\,\htmlId{3302}{\htmlClass{Symbol}{\text{)}}}\, \end{pmatrix}$ ,ENACT⦈
      record s { cc = just ((new ∪ˡ old)  removed  , q) , gid }

  Enact-NewConst :
    ───────────────────────────────────────
    $\begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Enact.html#2702}{\htmlId{3452}{\htmlClass{Generalizable}{\text{gid}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Enact.html#2680}{\htmlId{3458}{\htmlClass{Generalizable}{\text{t}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Enact.html#2727}{\htmlId{3462}{\htmlClass{Generalizable}{\text{ep}}}}\, \end{pmatrix}$  s ⇀⦇ $\begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Gov.Actions.html#647}{\htmlId{3476}{\htmlClass{InductiveConstructor}{\text{NewConstitution}}}}\, \\ \,\htmlId{3494}{\htmlClass{Symbol}{\text{(}}}\,\,\href{Ledger.Dijkstra.Specification.Enact.html#2583}{\htmlId{3495}{\htmlClass{Generalizable}{\text{dHash}}}}\, , \,\href{Ledger.Dijkstra.Specification.Enact.html#2604}{\htmlId{3503}{\htmlClass{Generalizable}{\text{sHash}}}}\,\,\htmlId{3508}{\htmlClass{Symbol}{\text{)}}}\, \end{pmatrix}$ ,ENACT⦈ record s { constitution = (dHash , sHash) , gid }

  Enact-HF :
    ───────────────────────────────────────
    $\begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Enact.html#2702}{\htmlId{3636}{\htmlClass{Generalizable}{\text{gid}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Enact.html#2680}{\htmlId{3642}{\htmlClass{Generalizable}{\text{t}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Enact.html#2727}{\htmlId{3646}{\htmlClass{Generalizable}{\text{ep}}}}\, \end{pmatrix}$  s ⇀⦇ $\begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Gov.Actions.html#685}{\htmlId{3660}{\htmlClass{InductiveConstructor}{\text{TriggerHardFork}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Enact.html#2634}{\htmlId{3678}{\htmlClass{Generalizable}{\text{protVer}}}}\, \end{pmatrix}$ ,ENACT⦈ record s { pv = protVer , gid }

  Enact-PParams :
    ───────────────────────────────────────
    $\begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Enact.html#2702}{\htmlId{3799}{\htmlClass{Generalizable}{\text{gid}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Enact.html#2680}{\htmlId{3805}{\htmlClass{Generalizable}{\text{t}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Enact.html#2727}{\htmlId{3809}{\htmlClass{Generalizable}{\text{ep}}}}\, \end{pmatrix}$  s ⇀⦇ $\begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Gov.Actions.html#723}{\htmlId{3823}{\htmlClass{InductiveConstructor}{\text{ChangePParams}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Enact.html#2483}{\htmlId{3839}{\htmlClass{Generalizable}{\text{up}}}}\, \end{pmatrix}$ ,ENACT⦈
      record s { pparams = applyUpdate (PParamsOf s) up , gid }

  Enact-Wdrl : let newWdrls = s .withdrawals ∪⁺ wdrls in
    ∑[ x  newWdrls ] x  t
    ───────────────────────────────────────
    $\begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Enact.html#2702}{\htmlId{4054}{\htmlClass{Generalizable}{\text{gid}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Enact.html#2680}{\htmlId{4060}{\htmlClass{Generalizable}{\text{t}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Enact.html#2727}{\htmlId{4064}{\htmlClass{Generalizable}{\text{ep}}}}\, \end{pmatrix}$  s ⇀⦇ $\begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Gov.Actions.html#761}{\htmlId{4078}{\htmlClass{InductiveConstructor}{\text{TreasuryWithdrawal}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Enact.html#2655}{\htmlId{4099}{\htmlClass{Generalizable}{\text{wdrls}}}}\, \end{pmatrix}$ ,ENACT⦈ record s { withdrawals = newWdrls }

  Enact-Info :
    ───────────────────────────────────────
    $\begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Enact.html#2702}{\htmlId{4219}{\htmlClass{Generalizable}{\text{gid}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Enact.html#2680}{\htmlId{4225}{\htmlClass{Generalizable}{\text{t}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Enact.html#2727}{\htmlId{4229}{\htmlClass{Generalizable}{\text{ep}}}}\, \end{pmatrix}$  s ⇀⦇ $\begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Gov.Actions.html#799}{\htmlId{4243}{\htmlClass{InductiveConstructor}{\text{Info}}}}\, \\ \,\htmlId{4250}{\htmlClass{Symbol}{\text{\_}}}\, \end{pmatrix}$ ,ENACT⦈ s