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

-- | Compact Simple indexes keep original file coordinates independently of retained order.
module Ecluse.Core.Registry.PyPI.Document (
    SimpleDocument,
    simpleDocument,
    simpleEnvelope,
    simpleFiles,
    restrictSimpleFiles,
    simpleSourceCompanions,
    simpleEncoding,

    -- * Packed full reads
    PackedSimple (..),
    packedSimple,
    packedSimpleDocument,
    packedSimplePlan,
    packedSimpleResident,
    urlHole,
) where

import Data.Aeson (Encoding, Object, Value, toEncoding)
import Data.Aeson.Encoding qualified as Encoding
import Data.Aeson.Key (Key)
import Data.Aeson.KeyMap qualified as KeyMap
import Data.Primitive.SmallArray (smallArrayFromList)
import Data.Set qualified as Set

import Ecluse.Core.Package.Entry (EntryKey)
import Ecluse.Core.Registry (CompanionRequirement (DeclaredCompanions))
import Ecluse.Core.Registry.Json.Packed (DocTable, Packed, Piece (..), Pieces (ArrayPieces), RenderPlan (..), packedResident, packedValue, tableResident)

-- | Supported envelope fields and files associated with their original source positions.
data SimpleDocument = SimpleDocument
    { SimpleDocument -> Object
simpleEnvelope :: Object
    -- ^ Supported top-level fields, excluding the file array.
    , SimpleDocument -> [(EntryKey, Value)]
simpleFiles :: [(EntryKey, Value)]
    -- ^ Original keys in source order, including gaps left by discarded files.
    }
    deriving stock (SimpleDocument -> SimpleDocument -> Bool
(SimpleDocument -> SimpleDocument -> Bool)
-> (SimpleDocument -> SimpleDocument -> Bool) -> Eq SimpleDocument
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SimpleDocument -> SimpleDocument -> Bool
== :: SimpleDocument -> SimpleDocument -> Bool
$c/= :: SimpleDocument -> SimpleDocument -> Bool
/= :: SimpleDocument -> SimpleDocument -> Bool
Eq, Int -> SimpleDocument -> ShowS
[SimpleDocument] -> ShowS
SimpleDocument -> String
(Int -> SimpleDocument -> ShowS)
-> (SimpleDocument -> String)
-> ([SimpleDocument] -> ShowS)
-> Show SimpleDocument
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SimpleDocument -> ShowS
showsPrec :: Int -> SimpleDocument -> ShowS
$cshow :: SimpleDocument -> String
show :: SimpleDocument -> String
$cshowList :: [SimpleDocument] -> ShowS
showList :: [SimpleDocument] -> ShowS
Show)

-- | Bind compact files to their source coordinates before admission or assembly.
simpleDocument :: Object -> [(EntryKey, Value)] -> SimpleDocument
simpleDocument :: Object -> [(EntryKey, Value)] -> SimpleDocument
simpleDocument Object
envelope = Object -> [(EntryKey, Value)] -> SimpleDocument
SimpleDocument (Key -> Object -> Object
forall v. Key -> KeyMap v -> KeyMap v
KeyMap.delete Key
"files" Object
envelope)

-- | Keep exact source coordinates, preserving the envelope, order and repeated matching entries.
restrictSimpleFiles :: Set EntryKey -> SimpleDocument -> SimpleDocument
restrictSimpleFiles :: Set EntryKey -> SimpleDocument -> SimpleDocument
restrictSimpleFiles Set EntryKey
keys SimpleDocument
document =
    Object -> [(EntryKey, Value)] -> SimpleDocument
simpleDocument (SimpleDocument -> Object
simpleEnvelope SimpleDocument
document) (((EntryKey, Value) -> Bool)
-> [(EntryKey, Value)] -> [(EntryKey, Value)]
forall a. (a -> Bool) -> [a] -> [a]
filter ((EntryKey -> Set EntryKey -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.member` Set EntryKey
keys) (EntryKey -> Bool)
-> ((EntryKey, Value) -> EntryKey) -> (EntryKey, Value) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (EntryKey, Value) -> EntryKey
forall a b. (a, b) -> a
fst) (SimpleDocument -> [(EntryKey, Value)]
simpleFiles SimpleDocument
document))

-- | A retained file needs a companion only where its entry declares core metadata.
simpleSourceCompanions :: CompanionRequirement
simpleSourceCompanions :: CompanionRequirement
simpleSourceCompanions = CompanionRequirement
DeclaredCompanions

-- The empty state avoids a trailing Series append. Strict payloads keep the fold eager.
data EnvelopePairs
    = NoEnvelopePairs
    | PendingFiles !Encoding.Series
    | CompletePairs !Encoding.Series

envelopePair :: Encoding.Series -> Key -> Value -> EnvelopePairs -> EnvelopePairs
envelopePair :: Series -> Key -> Value -> EnvelopePairs -> EnvelopePairs
envelopePair Series
files Key
key Value
value !EnvelopePairs
rest = case Key -> Key -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Key
key Key
"files" of
    Ordering
GT -> Series -> EnvelopePairs
PendingFiles (Series -> EnvelopePairs -> Series
prependPair (Key -> Value -> Series
encodeField Key
key Value
value) EnvelopePairs
rest)
    Ordering
EQ -> Series -> EnvelopePairs
CompletePairs (Series -> EnvelopePairs -> Series
prependPair Series
files EnvelopePairs
rest)
    Ordering
LT ->
        let !suffix :: Series
suffix = Series -> EnvelopePairs -> Series
finishEnvelope Series
files EnvelopePairs
rest
         in Series -> EnvelopePairs
CompletePairs (Key -> Value -> Series
encodeField Key
key Value
value Series -> Series -> Series
forall a. Semigroup a => a -> a -> a
<> Series
suffix)

finishEnvelope :: Encoding.Series -> EnvelopePairs -> Encoding.Series
finishEnvelope :: Series -> EnvelopePairs -> Series
finishEnvelope Series
files = \case
    EnvelopePairs
NoEnvelopePairs -> Series
files
    PendingFiles Series
rest -> Series
files Series -> Series -> Series
forall a. Semigroup a => a -> a -> a
<> Series
rest
    CompletePairs Series
pairs -> Series
pairs

prependPair :: Encoding.Series -> EnvelopePairs -> Encoding.Series
prependPair :: Series -> EnvelopePairs -> Series
prependPair !Series
pair = \case
    EnvelopePairs
NoEnvelopePairs -> Series
pair
    PendingFiles Series
rest -> Series
pair Series -> Series -> Series
forall a. Semigroup a => a -> a -> a
<> Series
rest
    CompletePairs Series
rest -> Series
pair Series -> Series -> Series
forall a. Semigroup a => a -> a -> a
<> Series
rest

encodeField :: Key -> Value -> Encoding.Series
encodeField :: Key -> Value -> Series
encodeField Key
key Value
value =
    let !encoding :: Encoding
encoding = Value -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding Value
value
     in Key -> Encoding -> Series
Encoding.pair Key
key Encoding
encoding

-- | Preserve envelope key order and file source order, overwriting any envelope files key.
simpleEncoding :: SimpleDocument -> Encoding
simpleEncoding :: SimpleDocument -> Encoding
simpleEncoding SimpleDocument
document
    | Object -> Bool
forall v. KeyMap v -> Bool
KeyMap.null Object
envelope = Series -> Encoding
Encoding.pairs Series
files
    | Bool
otherwise = Series -> Encoding
Encoding.pairs (Series -> EnvelopePairs -> Series
finishEnvelope Series
files ((Key -> Value -> EnvelopePairs -> EnvelopePairs)
-> EnvelopePairs -> Object -> EnvelopePairs
forall v a. (Key -> v -> a -> a) -> a -> KeyMap v -> a
KeyMap.foldrWithKey (Series -> Key -> Value -> EnvelopePairs -> EnvelopePairs
envelopePair Series
files) EnvelopePairs
NoEnvelopePairs Object
envelope))
  where
    envelope :: Object
envelope = SimpleDocument -> Object
simpleEnvelope SimpleDocument
document
    -- An unforced files encoder can retain the assembled document through a Series payload.
    !filesEncoding :: Encoding
filesEncoding = ((EntryKey, Value) -> Encoding) -> [(EntryKey, Value)] -> Encoding
forall a. (a -> Encoding) -> [a] -> Encoding
Encoding.list (Value -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding (Value -> Encoding)
-> ((EntryKey, Value) -> Value) -> (EntryKey, Value) -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (EntryKey, Value) -> Value
forall a b. (a, b) -> b
snd) (SimpleDocument -> [(EntryKey, Value)]
simpleFiles SimpleDocument
document)
    files :: Series
files = Key -> Encoding -> Series
Encoding.pair Key
"files" Encoding
filesEncoding

-- | A full read's envelope and packed files, with their original coordinates and shared string table.
data PackedSimple = PackedSimple
    { PackedSimple -> Object
packedEnvelope :: Object
    -- ^ Supported fields outside the file array.
    , PackedSimple -> DocTable
packedTable :: DocTable
    -- ^ The read's sealed strings, referenced by every file blob.
    , PackedSimple -> [(EntryKey, Packed)]
packedFiles :: [(EntryKey, Packed)]
    -- ^ Source coordinates in source order, including gaps from invalid files.
    }
    deriving stock (PackedSimple -> PackedSimple -> Bool
(PackedSimple -> PackedSimple -> Bool)
-> (PackedSimple -> PackedSimple -> Bool) -> Eq PackedSimple
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PackedSimple -> PackedSimple -> Bool
== :: PackedSimple -> PackedSimple -> Bool
$c/= :: PackedSimple -> PackedSimple -> Bool
/= :: PackedSimple -> PackedSimple -> Bool
Eq, Int -> PackedSimple -> ShowS
[PackedSimple] -> ShowS
PackedSimple -> String
(Int -> PackedSimple -> ShowS)
-> (PackedSimple -> String)
-> ([PackedSimple] -> ShowS)
-> Show PackedSimple
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PackedSimple -> ShowS
showsPrec :: Int -> PackedSimple -> ShowS
$cshow :: PackedSimple -> String
show :: PackedSimple -> String
$cshowList :: [PackedSimple] -> ShowS
showList :: [PackedSimple] -> ShowS
Show)

-- | Bind packed files to source coordinates, excluding any envelope file member.
packedSimple :: Object -> DocTable -> [(EntryKey, Packed)] -> PackedSimple
packedSimple :: Object -> DocTable -> [(EntryKey, Packed)] -> PackedSimple
packedSimple Object
envelope = Object -> DocTable -> [(EntryKey, Packed)] -> PackedSimple
PackedSimple (Key -> Object -> Object
forall v. Key -> KeyMap v -> KeyMap v
KeyMap.delete Key
"files" Object
envelope)

-- | Materialise the serving tree for tree-boundary callers, or refuse missing table strings.
packedSimpleDocument :: PackedSimple -> Maybe SimpleDocument
packedSimpleDocument :: PackedSimple -> Maybe SimpleDocument
packedSimpleDocument PackedSimple
document =
    Object -> [(EntryKey, Value)] -> SimpleDocument
simpleDocument (PackedSimple -> Object
packedEnvelope PackedSimple
document) ([(EntryKey, Value)] -> SimpleDocument)
-> Maybe [(EntryKey, Value)] -> Maybe SimpleDocument
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((EntryKey, Packed) -> Maybe (EntryKey, Value))
-> [(EntryKey, Packed)] -> Maybe [(EntryKey, Value)]
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) -> [a] -> f [b]
traverse (\(EntryKey
key, Packed
file) -> (EntryKey
key,) (Value -> (EntryKey, Value))
-> Maybe Value -> Maybe (EntryKey, Value)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> DocTable -> Packed -> Maybe Value
packedValue (PackedSimple -> DocTable
packedTable PackedSimple
document) Packed
file) (PackedSimple -> [(EntryKey, Packed)]
packedFiles PackedSimple
document)

-- | Render a source document in source order without reconstructing file trees or rebasing URLs.
packedSimplePlan :: PackedSimple -> RenderPlan
packedSimplePlan :: PackedSimple -> RenderPlan
packedSimplePlan PackedSimple
document =
    RenderPlan
        { planMembers :: Object
planMembers = PackedSimple -> Object
packedEnvelope PackedSimple
document
        , planSlot :: Key
planSlot = Key
"files"
        , planTables :: SmallArray DocTable
planTables = [DocTable] -> SmallArray DocTable
forall a. [a] -> SmallArray a
smallArrayFromList [PackedSimple -> DocTable
packedTable PackedSimple
document]
        , planPieces :: Pieces
planPieces = [Piece] -> Pieces
ArrayPieces [Int -> Packed -> Piece
Piece Int
0 Packed
file | (EntryKey
_, Packed
file) <- PackedSimple -> [(EntryKey, Packed)]
packedFiles PackedSimple
document]
        , planPrefix :: Maybe UrlPrefix
planPrefix = Maybe UrlPrefix
forall a. Maybe a
Nothing
        }

-- | Account for table and file storage, including each file's list cell, pair and coordinate.
packedSimpleResident :: PackedSimple -> Int
packedSimpleResident :: PackedSimple -> Int
packedSimpleResident PackedSimple
document = DocTable -> Int
tableResident (PackedSimple -> DocTable
packedTable PackedSimple
document) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ [Int] -> Int
forall a (f :: * -> *). (Foldable f, Num a) => f a -> a
sum [Int
64 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Packed -> Int
packedResident Packed
file | (EntryKey
_, Packed
file) <- PackedSimple -> [(EntryKey, Packed)]
packedFiles PackedSimple
document]

-- | The URL string a listing rebases onto its mount's distribution prefix.
urlHole :: [Text]
urlHole :: [Text]
urlHole = [Text
"url"]