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

-- | Complete compact-index scans quarantine conflicting coordinates before version grouping.
module Ecluse.Core.Registry.RubyGems.Reader (
    GemRead (..),
    GemReadResult (..),
    readGemInfoCharged,
    gemInfoSteps,
) where

import Data.Aeson (Value (Null))
import Data.ByteString qualified as BS
import Data.Map.Strict qualified as Map
import Data.Set qualified as Set

import Ecluse.Core.Package (InvalidEntry, InvalidEntryKind (InvalidVersionManifest), PackageInfo (..), PackageName, mkInvalidEntry)
import Ecluse.Core.Package.Entry (EntryKey (RecordEntry))
import Ecluse.Core.Registry.JsonStream (Steps (..), readStepsWithEnd)
import Ecluse.Core.Registry.Metadata.Fetch.Types (DocumentWalk)
import Ecluse.Core.Registry.Metadata.RecordStream (finishRecordSteps, recordFragmentCharge, recordSteps)
import Ecluse.Core.Registry.RubyGems.Document (GemDocument (..), GemRow (..))
import Ecluse.Core.Registry.RubyGems.Project (coordinatePlatform, coordinateVersion, unGemPlatform)
import Ecluse.Core.Registry.RubyGems.Projection (projectGemRows)
import Ecluse.Core.Registry.RubyGems.Wire (GemRowError (..), decodeInfoRow, infoRowIdentity, infoRowVersion)
import Ecluse.Core.Security (Limits, checkVersionCountOf)
import Ecluse.Core.Server.MemoryModel (saturatingCharge)
import Ecluse.Core.Strict (strictElements)
import Ecluse.Core.Version (Version, renderVersion)

-- | Selected reads retain all platforms and scan later rows for identity conflicts.
data GemRead
    = -- | Retain every supported artifact in the named package.
      FullRead
    | -- | Retain one exact raw version, without stopping the source scan early.
      SelectedRead Version
    deriving stock (GemRead -> GemRead -> Bool
(GemRead -> GemRead -> Bool)
-> (GemRead -> GemRead -> Bool) -> Eq GemRead
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: GemRead -> GemRead -> Bool
== :: GemRead -> GemRead -> Bool
$c/= :: GemRead -> GemRead -> Bool
/= :: GemRead -> GemRead -> Bool
Eq, Int -> GemRead -> ShowS
[GemRead] -> ShowS
GemRead -> String
(Int -> GemRead -> ShowS)
-> (GemRead -> String) -> ([GemRead] -> ShowS) -> Show GemRead
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> GemRead -> ShowS
showsPrec :: Int -> GemRead -> ShowS
$cshow :: GemRead -> String
show :: GemRead -> String
$cshowList :: [GemRead] -> ShowS
showList :: [GemRead] -> ShowS
Show)

-- | Native source rows and policy facts come from the same complete response.
data GemReadResult = GemReadResult
    { GemReadResult -> PackageInfo
gemReadInfo :: PackageInfo
    , GemReadResult -> GemDocument
gemReadDocument :: GemDocument
    }
    deriving stock (GemReadResult -> GemReadResult -> Bool
(GemReadResult -> GemReadResult -> Bool)
-> (GemReadResult -> GemReadResult -> Bool) -> Eq GemReadResult
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: GemReadResult -> GemReadResult -> Bool
== :: GemReadResult -> GemReadResult -> Bool
$c/= :: GemReadResult -> GemReadResult -> Bool
/= :: GemReadResult -> GemReadResult -> Bool
Eq, Int -> GemReadResult -> ShowS
[GemReadResult] -> ShowS
GemReadResult -> String
(Int -> GemReadResult -> ShowS)
-> (GemReadResult -> String)
-> ([GemReadResult] -> ShowS)
-> Show GemReadResult
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> GemReadResult -> ShowS
showsPrec :: Int -> GemReadResult -> ShowS
$cshow :: GemReadResult -> String
show :: GemReadResult -> String
$cshowList :: [GemReadResult] -> ShowS
showList :: [GemReadResult] -> ShowS
Show)

data Scan = Scan
    { Scan -> Int
scanOrdinal :: Int
    , Scan -> Bool
scanHeaderDone :: Bool
    , Scan -> [ByteString]
scanHeader :: [ByteString]
    , Scan -> Int
scanCount :: Int
    , Scan -> Map (Text, Text) Int
scanSeen :: Map (Text, Text) Int
    , Scan -> Set Text
scanBad :: Set Text
    , Scan -> [GemRow]
scanRows :: [GemRow]
    , Scan -> [InvalidEntry]
scanInvalid :: [InvalidEntry]
    , Scan -> Int
scanKeptCharge :: Int
    }

-- | Reserve row workspace before buffering and apply the complete upstream body ceiling.
readGemInfoCharged :: (Int -> IO ()) -> Limits -> PackageName -> Text -> GemRead -> DocumentWalk GemReadResult
readGemInfoCharged :: (Int -> IO ())
-> Limits
-> PackageName
-> Text
-> GemRead
-> DocumentWalk GemReadResult
readGemInfoCharged Int -> IO ()
pay Limits
limits PackageName
name Text
base GemRead
mode BodyLimit
bound InternTable
_ = (forall a. IO a -> IO a)
-> (Steps IO GemReadResult -> IO (Steps IO GemReadResult))
-> BodyLimit
-> Steps IO GemReadResult
-> IO ByteString
-> IO (Either LimitError (StreamResult GemReadResult))
forall (n :: * -> *) (m :: * -> *) s.
Monad n =>
(forall a. m a -> n a)
-> (Steps m s -> n (Steps m s))
-> BodyLimit
-> Steps m s
-> n ByteString
-> n (Either LimitError (StreamResult s))
readStepsWithEnd IO a -> IO a
forall a. a -> a
forall a. IO a -> IO a
id Steps IO GemReadResult -> IO (Steps IO GemReadResult)
forall (m :: * -> *) a. Monad m => Steps m a -> m (Steps m a)
finishRecordSteps BodyLimit
bound ((Int -> IO ())
-> Limits
-> PackageName
-> Text
-> GemRead
-> Steps IO GemReadResult
forall (m :: * -> *).
Monad m =>
(Int -> m ())
-> Limits
-> PackageName
-> Text
-> GemRead
-> Steps m GemReadResult
gemInfoSteps Int -> IO ()
pay Limits
limits PackageName
name Text
base GemRead
mode)

-- | Full and selected scans count every nonblank source row before parsing or version grouping.
gemInfoSteps :: (Monad m) => (Int -> m ()) -> Limits -> PackageName -> Text -> GemRead -> Steps m GemReadResult
gemInfoSteps :: forall (m :: * -> *).
Monad m =>
(Int -> m ())
-> Limits
-> PackageName
-> Text
-> GemRead
-> Steps m GemReadResult
gemInfoSteps Int -> m ()
pay Limits
limits PackageName
name Text
base GemRead
mode = (Scan -> Int -> ByteString -> m Int)
-> (Scan -> Int -> ByteString -> Either LimitError Scan)
-> (Scan -> Steps m GemReadResult)
-> Scan
-> Steps m GemReadResult
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 Scan -> Int -> ByteString -> m Int
reserve Scan -> Int -> ByteString -> Either LimitError Scan
consume Scan -> Steps m GemReadResult
forall {m :: * -> *}. Scan -> Steps m GemReadResult
complete Scan
initial
  where
    initial :: Scan
initial = Int
-> Bool
-> [ByteString]
-> Int
-> Map (Text, Text) Int
-> Set Text
-> [GemRow]
-> [InvalidEntry]
-> Int
-> Scan
Scan Int
0 Bool
False [] Int
0 Map (Text, Text) Int
forall k a. Map k a
Map.empty Set Text
forall a. Set a
Set.empty [] [] Int
0
    selected :: Text -> Bool
selected Text
raw = case GemRead
mode of GemRead
FullRead -> Bool
True; SelectedRead Version
version -> Text
raw Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Version -> Text
renderVersion Version
version
    reserve :: Scan -> Int -> ByteString -> m Int
reserve Scan
scan Int
previous ByteString
fragment = do
        let cost :: Int
cost = Int -> ByteString -> Int
recordFragmentCharge Int
previous ByteString
fragment
            bookkeeping :: Integer
bookkeeping = Integer
128 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Map (Text, Text) Int -> Int
forall k a. Map k a -> Int
Map.size (Scan -> Map (Text, Text) Int
scanSeen Scan
scan) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Set Text -> Int
forall a. Set a -> Int
Set.size (Scan -> Set Text
scanBad Scan
scan))
        Int -> m ()
pay (Integer -> Int
saturatingCharge (Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Scan -> Int
scanKeptCharge Scan
scan) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
cost Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
bookkeeping))
        Int -> m Int
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
cost
    consume :: Scan -> Int -> ByteString -> Either LimitError Scan
consume Scan
scan Int
charge ByteString
bytes
        | Bool -> Bool
not (Scan -> Bool
scanHeaderDone Scan
scan) = Scan -> Either LimitError Scan
forall a b. b -> Either a b
Right Scan
scan{scanOrdinal = scanOrdinal scan + 1, scanHeaderDone = bytes == "---", scanHeader = BS.copy bytes : scanHeader scan, scanKeptCharge = addCharge scan charge}
        | ByteString -> Bool
BS.null ByteString
bytes = Scan -> Either LimitError Scan
forall a b. b -> Either a b
Right Scan
scan{scanOrdinal = scanOrdinal scan + 1}
        | Bool
otherwise = do
            let count :: Int
count = Scan -> Int
scanCount Scan
scan Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
                key :: EntryKey
key = Int -> EntryKey
RecordEntry (Scan -> Int
scanOrdinal Scan
scan)
                claimed :: Maybe GemCoordinate
claimed = PackageName -> ByteString -> Maybe GemCoordinate
infoRowIdentity PackageName
name ByteString
bytes
                claimedVersion :: Maybe Text
claimedVersion = Version -> Text
renderVersion (Version -> Text) -> Maybe Version -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Maybe Version
infoRowVersion ByteString
bytes
                nativeIdentity :: Maybe (Text, Text)
nativeIdentity = (GemCoordinate -> (Text, Text))
-> Maybe GemCoordinate -> Maybe (Text, Text)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\GemCoordinate
coordinate -> (Version -> Text
renderVersion (GemCoordinate -> Version
coordinateVersion GemCoordinate
coordinate), GemPlatform -> Text
unGemPlatform (GemCoordinate -> GemPlatform
coordinatePlatform GemCoordinate
coordinate))) Maybe GemCoordinate
claimed
                seen :: Map (Text, Text) Int
seen = case Maybe (Text, Text)
nativeIdentity of
                    Just native :: (Text, Text)
native@(Text
raw, Text
_) | Text -> Bool
selected Text
raw -> (Int -> Int -> Int)
-> (Text, Text)
-> Int
-> Map (Text, Text) Int
-> Map (Text, Text) Int
forall k a. Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
Map.insertWith Int -> Int -> Int
forall a. Num a => a -> a -> a
(+) (Text, Text)
native Int
1 (Scan -> Map (Text, Text) Int
scanSeen Scan
scan)
                    Maybe (Text, Text)
_ -> Scan -> Map (Text, Text) Int
scanSeen Scan
scan
                next :: Scan
next = Scan
scan{scanOrdinal = scanOrdinal scan + 1, scanCount = count, scanSeen = seen}
            Limits -> Int -> Either LimitError ()
checkVersionCountOf Limits
limits Int
count
            case PackageName -> EntryKey -> ByteString -> Either GemRowError GemRow
decodeInfoRow PackageName
name EntryKey
key ByteString
bytes of
                Left GemRowError
fault ->
                    let reason :: Text
reason = case GemRowError
fault of GemRowMalformed Text
detail -> Text
detail; GemRowUnsupported Text
detail -> Text
detail
                        bad :: Set Text
bad = case Maybe Text
claimedVersion of Just Text
raw | Text -> Bool
selected Text
raw -> Text -> Set Text -> Set Text
forall a. Ord a => a -> Set a -> Set a
Set.insert Text
raw (Scan -> Set Text
scanBad Scan
next); Maybe Text
_ -> Scan -> Set Text
scanBad Scan
next
                        retain :: Bool
retain = case GemRead
mode of GemRead
FullRead -> Bool
True; SelectedRead Version
_ -> [InvalidEntry] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (Scan -> [InvalidEntry]
scanInvalid Scan
next)
                        invalid :: [InvalidEntry]
invalid = if Bool
retain then InvalidEntryKind -> Text -> Value -> Text -> InvalidEntry
mkInvalidEntry InvalidEntryKind
InvalidVersionManifest (EntryKey -> Text
forall b a. (Show a, IsString b) => a -> b
show EntryKey
key) Value
Null Text
reason InvalidEntry -> [InvalidEntry] -> [InvalidEntry]
forall a. a -> [a] -> [a]
: Scan -> [InvalidEntry]
scanInvalid Scan
next else Scan -> [InvalidEntry]
scanInvalid Scan
next
                     in Scan -> Either LimitError Scan
forall a b. b -> Either a b
Right Scan
next{scanBad = bad, scanInvalid = invalid, scanKeptCharge = if retain then addCharge next 256 else scanKeptCharge next}
                Right GemRow
row
                    | Text -> Bool
selected (Version -> Text
renderVersion (GemCoordinate -> Version
coordinateVersion (GemRow -> GemCoordinate
grCoordinate GemRow
row))) -> Scan -> Either LimitError Scan
forall a b. b -> Either a b
Right Scan
next{scanRows = row : scanRows next, scanKeptCharge = addCharge next charge}
                    | Bool
otherwise -> Scan -> Either LimitError Scan
forall a b. b -> Either a b
Right Scan
next
    addCharge :: Scan -> Int -> Int
    addCharge :: Scan -> Int -> Int
addCharge Scan
scan Int
charge = Integer -> Int
saturatingCharge (Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Scan -> Int
scanKeptCharge Scan
scan) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
charge)
    complete :: Scan -> Steps m GemReadResult
complete Scan
scan
        | Bool -> Bool
not (Scan -> Bool
scanHeaderDone Scan
scan) = Text -> Steps m GemReadResult
forall (m :: * -> *) s. Text -> Steps m s
Failed Text
"RubyGems info response has no header delimiter"
        | Bool
otherwise =
            let duplicateVersions :: Set Text
duplicateVersions = [Text] -> Set Text
forall a. Ord a => [a] -> Set a
Set.fromList [Text
raw | ((Text
raw, Text
_), Int
count) <- Map (Text, Text) Int -> [((Text, Text), Int)]
forall k a. Map k a -> [(k, a)]
Map.toList (Scan -> Map (Text, Text) Int
scanSeen Scan
scan), Int
count Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
1]
                bad :: Set Text
bad = Set Text -> Set Text -> Set Text
forall a. Ord a => Set a -> Set a -> Set a
Set.union (Scan -> Set Text
scanBad Scan
scan) Set Text
duplicateVersions
                rows :: [GemRow]
rows = [GemRow] -> [GemRow]
forall a. [a] -> [a]
reverse ((GemRow -> Bool) -> [GemRow] -> [GemRow]
forall a. (a -> Bool) -> [a] -> [a]
filter ((Text -> Set Text -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.notMember` Set Text
bad) (Text -> Bool) -> (GemRow -> Text) -> GemRow -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Version -> Text
renderVersion (Version -> Text) -> (GemRow -> Version) -> GemRow -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GemCoordinate -> Version
coordinateVersion (GemCoordinate -> Version)
-> (GemRow -> GemCoordinate) -> GemRow -> Version
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GemRow -> GemCoordinate
grCoordinate) (Scan -> [GemRow]
scanRows Scan
scan))
                conflicts :: [InvalidEntry]
conflicts = [InvalidEntryKind -> Text -> Value -> Text -> InvalidEntry
mkInvalidEntry InvalidEntryKind
InvalidVersionManifest Text
raw Value
Null Text
"conflicting or malformed RubyGems version artifacts" | Text
raw <- Set Text -> [Text]
forall a. Set a -> [a]
Set.toList Set Text
bad]
             in case PackageName -> Text -> [GemRow] -> Either MetadataError PackageInfo
projectGemRows PackageName
name Text
base [GemRow]
rows of
                    Left MetadataError
_ -> Text -> Steps m GemReadResult
forall (m :: * -> *) s. Text -> Steps m s
Failed Text
"RubyGems info projection failed"
                    Right PackageInfo
info -> GemReadResult -> Steps m GemReadResult
forall (m :: * -> *) s. s -> Steps m s
Finished (PackageInfo -> GemDocument -> GemReadResult
GemReadResult PackageInfo
info{infoInvalidEntries = strictElements (reverse (scanInvalid scan) <> conflicts)} (PackageName -> ByteString -> [GemRow] -> GemDocument
GemDocument PackageName
name (ByteString -> [ByteString] -> ByteString
BS.intercalate ByteString
"\n" ([ByteString] -> [ByteString]
forall a. [a] -> [a]
reverse (Scan -> [ByteString]
scanHeader Scan
scan)) ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"\n") [GemRow]
rows))