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

-- | Charged LF framing keeps native consumers responsible for record syntax and state.
module Ecluse.Core.Registry.Metadata.RecordStream (recordSteps, recordFragmentCharge, finishRecordSteps) where

import Data.ByteString qualified as BS

import Ecluse.Core.Registry.JsonStream (Steps (..))
import Ecluse.Core.Security (LimitError)
import Ecluse.Core.Server.MemoryModel (bufferedChunkCharge, decodedDocumentCharge, saturatingCharge)

-- | Charge fragments before retention and consume every physical record through EOF.
recordSteps :: (Monad m) => (s -> Int -> ByteString -> m Int) -> (s -> Int -> ByteString -> Either LimitError s) -> (s -> Steps m a) -> s -> Steps m a
recordSteps :: forall (m :: * -> *) s a.
Monad m =>
(s -> Int -> ByteString -> m Int)
-> (s -> Int -> ByteString -> Either LimitError s)
-> (s -> Steps m a)
-> s
-> Steps m a
recordSteps s -> Int -> ByteString -> m Int
reserve s -> Int -> ByteString -> Either LimitError s
consume s -> Steps m a
complete = [ByteString] -> Int -> s -> Steps m a
await [] Int
0
  where
    await :: [ByteString] -> Int -> s -> Steps m a
await [ByteString]
pieces Int
charge s
current = (ByteString -> m (Steps m a)) -> Steps m a
forall (m :: * -> *) s. (ByteString -> m (Steps m s)) -> Steps m s
NeedData (s -> [ByteString] -> Int -> ByteString -> m (Steps m a)
receive s
current [ByteString]
pieces Int
charge)
    receive :: s -> [ByteString] -> Int -> ByteString -> m (Steps m a)
receive s
current [ByteString]
pieces Int
charge ByteString
chunk
        | ByteString -> Bool
BS.null ByteString
chunk = Steps m a -> m (Steps m a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Steps m a -> m (Steps m a)) -> Steps m a -> m (Steps m a)
forall a b. (a -> b) -> a -> b
$ case [ByteString]
pieces of
            [] -> s -> Steps m a
complete s
current
            [ByteString]
_ -> (LimitError -> Steps m a)
-> (s -> Steps m a) -> Either LimitError s -> Steps m a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either LimitError -> Steps m a
forall (m :: * -> *) s. LimitError -> Steps m s
Refused s -> Steps m a
complete (s -> Int -> ByteString -> Either LimitError s
consume s
current Int
charge ([ByteString] -> ByteString
BS.concat ([ByteString] -> [ByteString]
forall a. [a] -> [a]
reverse [ByteString]
pieces)))
        | Bool
otherwise = s -> [ByteString] -> Int -> ByteString -> m (Steps m a)
splitLines s
current [ByteString]
pieces Int
charge ByteString
chunk
    splitLines :: s -> [ByteString] -> Int -> ByteString -> m (Steps m a)
splitLines !s
current [ByteString]
pieces Int
charge ByteString
chunk = case Word8 -> ByteString -> Maybe Int
BS.elemIndex Word8
10 ByteString
chunk of
        Maybe Int
Nothing -> do
            nextCharge <- s -> Int -> ByteString -> m Int
reserve s
current Int
charge ByteString
chunk
            pure (await (chunk : pieces) nextCharge current)
        Just Int
at -> do
            let (ByteString
line, ByteString
rest) = Int -> ByteString -> (ByteString, ByteString)
BS.splitAt Int
at ByteString
chunk
            nextCharge <- s -> Int -> ByteString -> m Int
reserve s
current Int
charge ByteString
line
            let record = case [ByteString]
pieces of [] -> ByteString
line; [ByteString]
_ -> [ByteString] -> ByteString
BS.concat ([ByteString] -> [ByteString]
forall a. [a] -> [a]
reverse (ByteString
line ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: [ByteString]
pieces))
            case consume current nextCharge record of
                Left LimitError
fault -> Steps m a -> m (Steps m a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (LimitError -> Steps m a
forall (m :: * -> *) s. LimitError -> Steps m s
Refused LimitError
fault)
                Right s
next
                    | ByteString -> Int
BS.length ByteString
rest Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1 -> Steps m a -> m (Steps m a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([ByteString] -> Int -> s -> Steps m a
await [] Int
0 s
next)
                    | Bool
otherwise -> s -> [ByteString] -> Int -> ByteString -> m (Steps m a)
splitLines s
next [] Int
0 (Int -> ByteString -> ByteString
BS.drop Int
1 ByteString
rest)

{- | The workspace of a partly buffered record once it holds this fragment: what earlier fragments
reserved, plus the fragment's decode and buffer charges, saturated at 'maxBound'.
-}
recordFragmentCharge :: Int -> ByteString -> Int
recordFragmentCharge :: Int -> ByteString -> Int
recordFragmentCharge Int
previous ByteString
fragment = Integer -> Int
saturatingCharge (Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
previous Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Int -> Integer
forall a. Integral a => a -> Integer
toInteger (ByteString -> Int
decodedDocumentCharge ByteString
fragment) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int -> Int
bufferedChunkCharge (ByteString -> Int
BS.length ByteString
fragment)))

-- | Close an unterminated final record without inventing a source byte.
finishRecordSteps :: (Monad m) => Steps m a -> m (Steps m a)
finishRecordSteps :: forall (m :: * -> *) a. Monad m => Steps m a -> m (Steps m a)
finishRecordSteps = \case
    NeedData ByteString -> m (Steps m a)
next -> ByteString -> m (Steps m a)
next ByteString
BS.empty
    Steps m a
step -> Steps m a -> m (Steps m a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Steps m a
step