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

-- | Preserve exact admitted installation rows in Compact Index responses.
module Ecluse.Core.Registry.RubyGems.Serving (
    assembleRubyGemsDocument,
    serialiseRubyGemsDocument,
) where

import Data.Aeson (Value (Null))
import Data.ByteString.Lazy qualified as LBS
import Data.Map.Strict qualified as Map

import Ecluse.Core.Package.Merge (MergePlan, SourceId)
import Ecluse.Core.Registry.CachedDocument (CachedDoc, npmCached, rubyGemsCached)
import Ecluse.Core.Registry.RubyGems.Document (GemDocument (..), GemRow (..))
import Ecluse.Core.Registry.ServedDocument (RenderRefused (RenderRefused), overlaySurvivors)
import Ecluse.Core.Snapshot (Snapshot, snapshotValue)

-- | Native order and physical coordinates follow the winning source snapshot.
assembleRubyGemsDocument :: Text -> Map SourceId (Snapshot CachedDoc) -> MergePlan -> Maybe CachedDoc -> CachedDoc
assembleRubyGemsDocument :: Text
-> Map SourceId (Snapshot CachedDoc)
-> MergePlan
-> Maybe CachedDoc
-> CachedDoc
assembleRubyGemsDocument Text
_ Map SourceId (Snapshot CachedDoc)
bySource MergePlan
plan Maybe CachedDoc
base = case Maybe GemDocument
anchor of
    -- No anchor gives no native name or header, so serialiseRubyGemsDocument refuses this foreign sentinel.
    Maybe GemDocument
Nothing -> (Value -> CachedDoc, CachedDoc -> Maybe Value)
-> Value -> CachedDoc
forall a b. (a, b) -> a
fst (Value -> CachedDoc, CachedDoc -> Maybe Value)
npmCached Value
Null
    Just GemDocument
document -> (GemDocument -> CachedDoc, CachedDoc -> Maybe GemDocument)
-> GemDocument -> CachedDoc
forall a b. (a, b) -> a
fst (GemDocument -> CachedDoc, CachedDoc -> Maybe GemDocument)
rubyGemsCached GemDocument
document{gemRows = map snd (overlaySurvivors entries native plan)}
  where
    native :: Map SourceId (Snapshot GemDocument)
native = (Snapshot CachedDoc -> Maybe (Snapshot GemDocument))
-> Map SourceId (Snapshot CachedDoc)
-> Map SourceId (Snapshot GemDocument)
forall a b k. (a -> Maybe b) -> Map k a -> Map k b
Map.mapMaybe ((CachedDoc -> Maybe GemDocument)
-> Snapshot CachedDoc -> Maybe (Snapshot GemDocument)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Snapshot a -> f (Snapshot b)
traverse ((GemDocument -> CachedDoc, CachedDoc -> Maybe GemDocument)
-> CachedDoc -> Maybe GemDocument
forall a b. (a, b) -> b
snd (GemDocument -> CachedDoc, CachedDoc -> Maybe GemDocument)
rubyGemsCached)) Map SourceId (Snapshot CachedDoc)
bySource
    anchor :: Maybe GemDocument
anchor = (Maybe CachedDoc
base Maybe CachedDoc
-> (CachedDoc -> Maybe GemDocument) -> Maybe GemDocument
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (GemDocument -> CachedDoc, CachedDoc -> Maybe GemDocument)
-> CachedDoc -> Maybe GemDocument
forall a b. (a, b) -> b
snd (GemDocument -> CachedDoc, CachedDoc -> Maybe GemDocument)
rubyGemsCached) Maybe GemDocument -> Maybe GemDocument -> Maybe GemDocument
forall a. Maybe a -> Maybe a -> Maybe a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Snapshot GemDocument -> GemDocument
forall a. Snapshot a -> a
snapshotValue (Snapshot GemDocument -> GemDocument)
-> ((SourceId, Snapshot GemDocument) -> Snapshot GemDocument)
-> (SourceId, Snapshot GemDocument)
-> GemDocument
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (SourceId, Snapshot GemDocument) -> Snapshot GemDocument
forall a b. (a, b) -> b
snd ((SourceId, Snapshot GemDocument) -> GemDocument)
-> Maybe (SourceId, Snapshot GemDocument) -> Maybe GemDocument
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Map SourceId (Snapshot GemDocument)
-> Maybe (SourceId, Snapshot GemDocument)
forall k a. Map k a -> Maybe (k, a)
Map.lookupMin Map SourceId (Snapshot GemDocument)
native)
    entries :: GemDocument -> [(EntryKey, GemRow)]
entries GemDocument
document = [(GemRow -> EntryKey
grEntryKey GemRow
row, GemRow
row) | GemRow
row <- GemDocument -> [GemRow]
gemRows GemDocument
document]

-- | Encoding refuses foreign documents rather than emitting a non-native response.
serialiseRubyGemsDocument :: CachedDoc -> Either RenderRefused LByteString
serialiseRubyGemsDocument :: CachedDoc -> Either RenderRefused LByteString
serialiseRubyGemsDocument CachedDoc
cached = do
    document <- RenderRefused
-> Maybe GemDocument -> Either RenderRefused GemDocument
forall l r. l -> Maybe r -> Either l r
maybeToRight RenderRefused
RenderRefused ((GemDocument -> CachedDoc, CachedDoc -> Maybe GemDocument)
-> CachedDoc -> Maybe GemDocument
forall a b. (a, b) -> b
snd (GemDocument -> CachedDoc, CachedDoc -> Maybe GemDocument)
rubyGemsCached CachedDoc
cached)
    pure (LBS.fromChunks (gemHeader document : concatMap (\GemRow
row -> [GemRow -> StrictByteString
grSource GemRow
row, StrictByteString
"\n"]) (gemRows document)))