-- SPDX-FileCopyrightText: 2026 Alexandra de Wit
--
-- SPDX-License-Identifier: MIT

{- | The solver's working records, the vocabulary "Ecluse.Composition.MemoryPlan" passes between
resolving the tenant demands, walking the shed ladder, and rendering the boot lines. Their
constructors stay out of that module's public contract, so this is the only module exporting them,
for its own cluster and for the specs that build a record directly. Importing it opts out of the
public module's stability promise, as @text@ does.
-}
module Ecluse.Composition.MemoryPlan.Internal (
    PlanInputs (..),
    TenantDemands (..),
    OverridePins (..),
    ShedOutcomes (..),
) where

import Ecluse.Composition.MemoryPlan.Types (QueueTenantDemand)
import Ecluse.Config (CacheSettings, LimitsSettings, QueueSettings)

{- | Everything resolved before the plan knows whether a heap ceiling exists. The solved
path and the fallback path both read it.
-}
data PlanInputs = PlanInputs
    { PlanInputs -> CacheSettings
piCache :: CacheSettings
    , PlanInputs -> LimitsSettings
piLimits :: LimitsSettings
    , PlanInputs -> QueueSettings
piQueue :: QueueSettings
    , PlanInputs -> Maybe Int
piExplicitAdmission :: Maybe Int
    , PlanInputs -> Bool
piPublishConfigured :: Bool
    , PlanInputs -> QueueTenantDemand
piQueueDemand :: QueueTenantDemand
    , PlanInputs -> Int
piCapabilities :: Int
    , PlanInputs -> Int
piAllocAreaBytes :: Int
    , PlanInputs -> Int
piCpuAdmission :: Int
    , PlanInputs -> Text
piCpuAdmissionLine :: Text
    , PlanInputs -> Text
piCeilingClause :: Text
    }

{- | Every tenant's demand before the shed ladder walks it, resolved from the ceiling: the
byte charges, plus the counts, pins, and flags the ladder's steps read.
-}
data TenantDemands = TenantDemands
    { TenantDemands -> Int
tdCeiling :: Int
    , TenantDemands -> Int
tdReserve :: Int
    , TenantDemands -> Int
tdFixedBuffers :: Int
    , TenantDemands -> OverridePins
tdPins :: OverridePins
    , TenantDemands -> Int
tdCacheDesired :: Int
    , TenantDemands -> Maybe Int
tdCacheEntriesExplicit :: Maybe Int
    -- ^ Cache entries, not bytes.
    , TenantDemands -> Int
tdResponseFinal :: Int
    , TenantDemands -> Int
tdAdmissionDesired :: Int
    -- ^ Concurrent serve operations, not bytes.
    , TenantDemands -> Bool
tdPublishConfigured :: Bool
    , TenantDemands -> Int
tdPublishDesired :: Int
    , TenantDemands -> Int
tdRequestFinal :: Int
    , TenantDemands -> Int
tdRequestComputed :: Int
    , TenantDemands -> Int
tdDepthDesired :: Int
    -- ^ Queued jobs, not bytes.
    , TenantDemands -> Bool
tdMemoryBacked :: Bool
    , TenantDemands -> Bool
tdMirrors :: Bool
    , TenantDemands -> Int
tdArtifactCapDesired :: Int
    , TenantDemands -> Int
tdMirrorChargeDesired :: Int
    }

{- | Explicit overrides, each pinned ('Just') or substituted out ('Nothing'), in the plan's
allocation order. A pinned bound never sheds and answers for its own overshoot.
-}
data OverridePins = OverridePins
    { OverridePins -> Maybe Int
opCache :: Maybe Int
    , OverridePins -> Maybe Int
opAdmission :: Maybe Int
    , OverridePins -> Maybe Int
opResponse :: Maybe Int
    , OverridePins -> Maybe Int
opRequest :: Maybe Int
    , OverridePins -> Maybe Int
opDepth :: Maybe Int
    , OverridePins -> Maybe Int
opArtifact :: Maybe Int
    }
    deriving stock (OverridePins -> OverridePins -> Bool
(OverridePins -> OverridePins -> Bool)
-> (OverridePins -> OverridePins -> Bool) -> Eq OverridePins
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: OverridePins -> OverridePins -> Bool
== :: OverridePins -> OverridePins -> Bool
$c/= :: OverridePins -> OverridePins -> Bool
/= :: OverridePins -> OverridePins -> Bool
Eq, Int -> OverridePins -> ShowS
[OverridePins] -> ShowS
OverridePins -> String
(Int -> OverridePins -> ShowS)
-> (OverridePins -> String)
-> ([OverridePins] -> ShowS)
-> Show OverridePins
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> OverridePins -> ShowS
showsPrec :: Int -> OverridePins -> ShowS
$cshow :: OverridePins -> String
show :: OverridePins -> String
$cshowList :: [OverridePins] -> ShowS
showList :: [OverridePins] -> ShowS
Show)

{- | Every tenant's post-shed value plus the residual overshoot the ladder could not
reclaim. The combined invariant is a pure function of this record.
-}
data ShedOutcomes = ShedOutcomes
    { ShedOutcomes -> Int
soMirrorShed :: Int
    , ShedOutcomes -> Int
soArtifactCapFinal :: Int
    , ShedOutcomes -> Int
soCacheShed :: Int
    , ShedOutcomes -> Int
soCacheFinal :: Int
    , ShedOutcomes -> Int
soAdmissionFinal :: Int
    , ShedOutcomes -> Int
soResponseFinal :: Int
    , ShedOutcomes -> Int
soPublishShed :: Int
    , ShedOutcomes -> Int
soPublishFinal :: Int
    , ShedOutcomes -> Int
soQueueShedBytes :: Int
    , ShedOutcomes -> Int
soDepthFinal :: Int
    , ShedOutcomes -> Int
soQueueTenantBytes :: Int
    , ShedOutcomes -> Int
soResidualOvershoot :: Int
    }