-- SPDX-FileCopyrightText: 2026 Alexandra de Wit
--
-- SPDX-License-Identifier: MIT
{-# LANGUAGE RoleAnnotations #-}

-- | Origin coordinates, limits, and credential posture for registry data-plane reads.
module Ecluse.Core.Registry.Origin (
    OriginClient (..),
    originClient,
    ocBaseUrl,
    originBaseUrl,

    -- * Credential posture, carried in the type
    OriginFor,
    Public,
    Private,
    anonymousOrigin,
    perCallerOrigin,
    originClientOf,
    chargingFullReads,
    chargingFullReadWorkspace,
    chargingSelectedMetadata,
    chargingNativeReads,
    controlPublicOrigin,
) where

import Ecluse.Core.Credential (ClientCredential)
import Ecluse.Core.Registry.Exchange (accumulatingCharge)
import Ecluse.Core.Registry.Exchange.Types (ExchangeClient)
import Ecluse.Core.Registry.Public (PublicControl)
import Ecluse.Core.Registry.Request (BaseRequest, baseRequestUrl, mkBaseRequest)
import Ecluse.Core.Security (Limits)
import Ecluse.Core.Security.Egress (RegistryUrl, registryUrlText)
import Ecluse.Core.Server.Admission.Budget (scaleCharge)
import Ecluse.Core.Server.Admission.Types (ChargeFactors (cfFullReadPermille))
import Ecluse.Core.Telemetry.Upstream (PublicReadKind)

-- | One origin's coordinates, credential posture, and response bound.
data OriginClient = OriginClient
    { OriginClient -> BaseRequest
ocBaseRequest :: BaseRequest
    -- ^ The https-only egress witness the proxy appends a package path to, parsed once.
    , OriginClient -> ExchangeClient
ocClient :: ExchangeClient
    -- ^ The shared connection pool and deadline sweeper to issue requests through.
    , OriginClient -> Maybe ClientCredential
ocToken :: Maybe ClientCredential
    -- ^ 'Nothing' for an anonymous origin. A passthrough read carries the caller's own verbatim.
    , OriginClient -> Limits
ocLimits :: Limits
    -- ^ The bound every read through this origin is held to, fail-closed past the maximum.
    , OriginClient -> Maybe PublicControl
ocPublicControl :: Maybe PublicControl
    , OriginClient -> Maybe PublicReadKind
ocReadKind :: Maybe PublicReadKind
    , OriginClient -> Int -> IO ()
ocChargeSelectedRead :: Int -> IO ()
    -- ^ Reserve the native selected reader's absolute workspace before allocation.
    , OriginClient -> Int -> IO ()
ocChargeFullReadWorkspace :: Int -> IO ()
    -- ^ Reserve the full reader's absolute row workspace separately from retained source bytes.
    , OriginClient -> Int -> IO ()
ocChargeFullRead :: Int -> IO ()
    {- ^ Pays each decompressed chunk's own charge, never a running total, before the parser sees it.
    A high-water payer must be wrapped in 'accumulatingCharge'.
    -}
    }

-- | Build an origin with caller-supplied limits, transport, and credential posture.
originClient :: Limits -> ExchangeClient -> RegistryUrl -> Maybe ClientCredential -> OriginClient
originClient :: Limits
-> ExchangeClient
-> RegistryUrl
-> Maybe ClientCredential
-> OriginClient
originClient Limits
limits ExchangeClient
client = Limits
-> ExchangeClient
-> BaseRequest
-> Maybe ClientCredential
-> OriginClient
originAt Limits
limits ExchangeClient
client (BaseRequest -> Maybe ClientCredential -> OriginClient)
-> (RegistryUrl -> BaseRequest)
-> RegistryUrl
-> Maybe ClientCredential
-> OriginClient
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RegistryUrl -> BaseRequest
mkBaseRequest

originAt :: Limits -> ExchangeClient -> BaseRequest -> Maybe ClientCredential -> OriginClient
originAt :: Limits
-> ExchangeClient
-> BaseRequest
-> Maybe ClientCredential
-> OriginClient
originAt Limits
limits ExchangeClient
client BaseRequest
base Maybe ClientCredential
token =
    OriginClient{ocBaseRequest :: BaseRequest
ocBaseRequest = BaseRequest
base, ocClient :: ExchangeClient
ocClient = ExchangeClient
client, ocToken :: Maybe ClientCredential
ocToken = Maybe ClientCredential
token, ocLimits :: Limits
ocLimits = Limits
limits, ocChargeFullRead :: Int -> IO ()
ocChargeFullRead = IO () -> Int -> IO ()
forall a b. a -> b -> a
const IO ()
forall (f :: * -> *). Applicative f => f ()
pass, ocChargeFullReadWorkspace :: Int -> IO ()
ocChargeFullReadWorkspace = IO () -> Int -> IO ()
forall a b. a -> b -> a
const IO ()
forall (f :: * -> *). Applicative f => f ()
pass, ocChargeSelectedRead :: Int -> IO ()
ocChargeSelectedRead = IO () -> Int -> IO ()
forall a b. a -> b -> a
const IO ()
forall (f :: * -> *). Applicative f => f ()
pass, ocPublicControl :: Maybe PublicControl
ocPublicControl = Maybe PublicControl
forall a. Maybe a
Nothing, ocReadKind :: Maybe PublicReadKind
ocReadKind = Maybe PublicReadKind
forall a. Maybe a
Nothing}

-- | The origin's base URL, read from its one parse so the two cannot disagree.
ocBaseUrl :: OriginClient -> RegistryUrl
ocBaseUrl :: OriginClient -> RegistryUrl
ocBaseUrl = BaseRequest -> RegistryUrl
baseRequestUrl (BaseRequest -> RegistryUrl)
-> (OriginClient -> BaseRequest) -> OriginClient -> RegistryUrl
forall b c a. (b -> c) -> (a -> b) -> a -> c
. OriginClient -> BaseRequest
ocBaseRequest

-- | The origin's base URL as text.
originBaseUrl :: OriginClient -> Text
originBaseUrl :: OriginClient -> Text
originBaseUrl = RegistryUrl -> Text
registryUrlText (RegistryUrl -> Text)
-> (OriginClient -> RegistryUrl) -> OriginClient -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. OriginClient -> RegistryUrl
ocBaseUrl

-- | Retain credential posture in the type. The nominal role prevents retagging with 'coerce'.
newtype OriginFor (posture :: Type) = OriginFor OriginClient

-- RoleAnnotations is not in GHC2021. Without this line the parameter takes GHC's phantom role,
-- and coerce changes it from any module, constructor in scope or not.
type role OriginFor nominal

-- The two postures. Neither type is inhabited: each names a posture in a type, never a value.
data Public
data Private

-- | An origin dialled with no credential, so no caller's authorisation scopes what it reads.
anonymousOrigin :: Limits -> ExchangeClient -> BaseRequest -> OriginFor Public
anonymousOrigin :: Limits -> ExchangeClient -> BaseRequest -> OriginFor Public
anonymousOrigin Limits
limits ExchangeClient
client BaseRequest
base = OriginClient -> OriginFor Public
forall posture. OriginClient -> OriginFor posture
OriginFor (Limits
-> ExchangeClient
-> BaseRequest
-> Maybe ClientCredential
-> OriginClient
originAt Limits
limits ExchangeClient
client BaseRequest
base Maybe ClientCredential
forall a. Maybe a
Nothing)

-- | An origin presenting one caller's credential, so what it reads is scoped to that caller.
perCallerOrigin :: Limits -> ExchangeClient -> BaseRequest -> Maybe ClientCredential -> OriginFor Private
perCallerOrigin :: Limits
-> ExchangeClient
-> BaseRequest
-> Maybe ClientCredential
-> OriginFor Private
perCallerOrigin Limits
limits ExchangeClient
client BaseRequest
base Maybe ClientCredential
token = OriginClient -> OriginFor Private
forall posture. OriginClient -> OriginFor posture
OriginFor (Limits
-> ExchangeClient
-> BaseRequest
-> Maybe ClientCredential
-> OriginClient
originAt Limits
limits ExchangeClient
client BaseRequest
base Maybe ClientCredential
token)

-- | The plain record behind a tagged origin, for the operations that take any origin.
originClientOf :: OriginFor posture -> OriginClient
originClientOf :: forall posture. OriginFor posture -> OriginClient
originClientOf (OriginFor OriginClient
client) = OriginClient
client

{- | The same origin, whose full reads hand the payer each chunk's own charge, never a running total.
A high-water payer must be wrapped in 'accumulatingCharge'.
-}
chargingFullReads :: (Int -> IO ()) -> OriginFor posture -> OriginFor posture
chargingFullReads :: forall posture.
(Int -> IO ()) -> OriginFor posture -> OriginFor posture
chargingFullReads Int -> IO ()
charge (OriginFor OriginClient
client) = OriginClient -> OriginFor posture
forall posture. OriginClient -> OriginFor posture
OriginFor OriginClient
client{ocChargeFullRead = charge}

-- | Bind a full reader's absolute workspace without replacing its incremental source charge.
chargingFullReadWorkspace :: (Int -> IO ()) -> OriginFor posture -> OriginFor posture
chargingFullReadWorkspace :: forall posture.
(Int -> IO ()) -> OriginFor posture -> OriginFor posture
chargingFullReadWorkspace Int -> IO ()
pay (OriginFor OriginClient
client) = OriginClient -> OriginFor posture
forall posture. OriginClient -> OriginFor posture
OriginFor OriginClient
client{ocChargeFullReadWorkspace = pay}

-- | Bind an absolute workspace reservation for native selected metadata reads.
chargingSelectedMetadata :: (Int -> IO ()) -> OriginFor posture -> OriginFor posture
chargingSelectedMetadata :: forall posture.
(Int -> IO ()) -> OriginFor posture -> OriginFor posture
chargingSelectedMetadata Int -> IO ()
pay (OriginFor OriginClient
client) = OriginClient -> OriginFor posture
forall posture. OriginClient -> OriginFor posture
OriginFor OriginClient
client{ocChargeSelectedRead = pay}

{- | Bind one high-water payer to a native read: the selected workspace at face value, and the
running total of the incremental full-read charges at the full-read factor.
-}
chargingNativeReads :: ChargeFactors -> (Int -> IO ()) -> OriginFor posture -> IO (OriginFor posture)
chargingNativeReads :: forall posture.
ChargeFactors
-> (Int -> IO ()) -> OriginFor posture -> IO (OriginFor posture)
chargingNativeReads ChargeFactors
factors Int -> IO ()
pay OriginFor posture
origin = do
    fullRead <- (Int -> IO ()) -> IO (Int -> IO ())
accumulatingCharge (Int -> IO ()
pay (Int -> IO ()) -> (Int -> Int) -> Int -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int -> Int
scaleCharge (ChargeFactors -> Int
cfFullReadPermille ChargeFactors
factors))
    pure (chargingSelectedMetadata pay (chargingFullReads fullRead origin))

-- | Control anonymous public reads, optionally assigning one kind to both metadata operations.
controlPublicOrigin :: PublicControl -> Maybe PublicReadKind -> OriginFor Public -> OriginFor Public
controlPublicOrigin :: PublicControl
-> Maybe PublicReadKind -> OriginFor Public -> OriginFor Public
controlPublicOrigin PublicControl
control Maybe PublicReadKind
kind (OriginFor OriginClient
client) = OriginClient -> OriginFor Public
forall posture. OriginClient -> OriginFor posture
OriginFor OriginClient
client{ocPublicControl = Just control, ocReadKind = kind}