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

-- | Retained RubyGems installation metadata stays separate from grouped policy facts.
module Ecluse.Core.Registry.RubyGems.Document (
    GemRequirement,
    GemDependency (..),
    GemRow (..),
    GemDocument (..),
    restrictGemDocument,
    selectGemVersion,
    gemDocumentBytes,
) where

import Data.ByteString qualified as BS
import Data.Map.Strict qualified as Map
import Data.Set qualified as Set
import Data.Time (UTCTime)

import Ecluse.Core.Package (Hash, PackageName, hashValue, renderPackageName)
import Ecluse.Core.Package.Entry (EntryKey)
import Ecluse.Core.Registry.RubyGems.Project (GemCoordinate, coordinateFilename, coordinatePlatform, coordinateVersion, unGemPlatform)
import Ecluse.Core.Server.Path (unFilename)
import Ecluse.Core.Text (textStorageBytes)
import Ecluse.Core.Version (Version, renderVersion)

-- | Conjunctive native clauses retain their exact operators and version spelling.
type GemRequirement = NonEmpty Text

-- | A runtime dependency and every conjunctive requirement on that dependency.
data GemDependency = GemDependency
    { GemDependency -> PackageName
gdName :: PackageName
    , GemDependency -> GemRequirement
gdRequirement :: GemRequirement
    }
    deriving stock (GemDependency -> GemDependency -> Bool
(GemDependency -> GemDependency -> Bool)
-> (GemDependency -> GemDependency -> Bool) -> Eq GemDependency
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: GemDependency -> GemDependency -> Bool
== :: GemDependency -> GemDependency -> Bool
$c/= :: GemDependency -> GemDependency -> Bool
/= :: GemDependency -> GemDependency -> Bool
Eq, Int -> GemDependency -> ShowS
[GemDependency] -> ShowS
GemDependency -> String
(Int -> GemDependency -> ShowS)
-> (GemDependency -> String)
-> ([GemDependency] -> ShowS)
-> Show GemDependency
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> GemDependency -> ShowS
showsPrec :: Int -> GemDependency -> ShowS
$cshow :: GemDependency -> String
show :: GemDependency -> String
$cshowList :: [GemDependency] -> ShowS
showList :: [GemDependency] -> ShowS
Show)

-- | One artifact retains its original physical position and independently owned source bytes.
data GemRow = GemRow
    { GemRow -> EntryKey
grEntryKey :: EntryKey
    , GemRow -> GemCoordinate
grCoordinate :: GemCoordinate
    , GemRow -> [GemDependency]
grDependencies :: [GemDependency]
    , GemRow -> GemRequirement
grRequiredRuby :: GemRequirement
    , GemRow -> GemRequirement
grRequiredRubyGems :: GemRequirement
    , GemRow -> Hash
grChecksum :: Hash
    , GemRow -> Maybe UTCTime
grPublishedAt :: Maybe UTCTime
    , GemRow -> Map Text Text
grExtras :: Map Text Text
    , GemRow -> ByteString
grSource :: ByteString
    }
    deriving stock (GemRow -> GemRow -> Bool
(GemRow -> GemRow -> Bool)
-> (GemRow -> GemRow -> Bool) -> Eq GemRow
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: GemRow -> GemRow -> Bool
== :: GemRow -> GemRow -> Bool
$c/= :: GemRow -> GemRow -> Bool
/= :: GemRow -> GemRow -> Bool
Eq, Int -> GemRow -> ShowS
[GemRow] -> ShowS
GemRow -> String
(Int -> GemRow -> ShowS)
-> (GemRow -> String) -> ([GemRow] -> ShowS) -> Show GemRow
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> GemRow -> ShowS
showsPrec :: Int -> GemRow -> ShowS
$cshow :: GemRow -> String
show :: GemRow -> String
$cshowList :: [GemRow] -> ShowS
showList :: [GemRow] -> ShowS
Show)

-- | Ordered per-package rows preserve platform distinctions without fetching compact specifications.
data GemDocument = GemDocument
    { GemDocument -> PackageName
gemName :: PackageName
    , GemDocument -> ByteString
gemHeader :: ByteString
    , GemDocument -> [GemRow]
gemRows :: [GemRow]
    }
    deriving stock (GemDocument -> GemDocument -> Bool
(GemDocument -> GemDocument -> Bool)
-> (GemDocument -> GemDocument -> Bool) -> Eq GemDocument
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: GemDocument -> GemDocument -> Bool
== :: GemDocument -> GemDocument -> Bool
$c/= :: GemDocument -> GemDocument -> Bool
/= :: GemDocument -> GemDocument -> Bool
Eq, Int -> GemDocument -> ShowS
[GemDocument] -> ShowS
GemDocument -> String
(Int -> GemDocument -> ShowS)
-> (GemDocument -> String)
-> ([GemDocument] -> ShowS)
-> Show GemDocument
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> GemDocument -> ShowS
showsPrec :: Int -> GemDocument -> ShowS
$cshow :: GemDocument -> String
show :: GemDocument -> String
$cshowList :: [GemDocument] -> ShowS
showList :: [GemDocument] -> ShowS
Show)

-- | Keep only artifacts authorised by their original source positions.
restrictGemDocument :: Set EntryKey -> GemDocument -> GemDocument
restrictGemDocument :: Set EntryKey -> GemDocument -> GemDocument
restrictGemDocument Set EntryKey
admitted GemDocument
document = GemDocument
document{gemRows = filter ((`Set.member` admitted) . grEntryKey) (gemRows document)}

-- | Retain every platform row for one exact raw version, without comparison-key aliases.
selectGemVersion :: Version -> GemDocument -> Maybe GemDocument
selectGemVersion :: Version -> GemDocument -> Maybe GemDocument
selectGemVersion Version
version GemDocument
document = do
    let rows :: [GemRow]
rows = (GemRow -> Bool) -> [GemRow] -> [GemRow]
forall a. (a -> Bool) -> [a] -> [a]
filter ((Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Version -> Text
renderVersion Version
version) (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) (GemDocument -> [GemRow]
gemRows GemDocument
document)
    Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> Bool
not ([GemRow] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [GemRow]
rows))
    GemDocument -> Maybe GemDocument
forall a. a -> Maybe a
forall (f :: * -> *) a. Applicative f => a -> f a
pure GemDocument
document{gemRows = rows}

{- | Cache units for a retained document. Every row carries the document's gem name, so the name is
measured once and counted per row.
-}
gemDocumentBytes :: GemDocument -> Int64
gemDocumentBytes :: GemDocument -> Int64
gemDocumentBytes GemDocument
document = Int64
64 Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Int64
nameBytes Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
BS.length (GemDocument -> ByteString
gemHeader GemDocument
document)) Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ [Int64] -> Int64
forall a (f :: * -> *). (Foldable f, Num a) => f a -> a
sum ((GemRow -> Int64) -> [GemRow] -> [Int64]
forall a b. (a -> b) -> [a] -> [b]
map GemRow -> Int64
rowBytes (GemDocument -> [GemRow]
gemRows GemDocument
document))
  where
    nameBytes :: Int64
nameBytes = Text -> Int64
nativeTextBytes (PackageName -> Text
renderPackageName (GemDocument -> PackageName
gemName GemDocument
document))
    rowBytes :: GemRow -> Int64
rowBytes GemRow
row =
        let coordinate :: GemCoordinate
coordinate = GemRow -> GemCoordinate
grCoordinate GemRow
row
            strings :: [Text]
strings = [Version -> Text
renderVersion (GemCoordinate -> Version
coordinateVersion GemCoordinate
coordinate), GemPlatform -> Text
unGemPlatform (GemCoordinate -> GemPlatform
coordinatePlatform GemCoordinate
coordinate), Filename -> Text
unFilename (GemCoordinate -> Filename
coordinateFilename GemCoordinate
coordinate), Hash -> Text
hashValue (GemRow -> Hash
grChecksum GemRow
row)]
            requirementBytes :: t Text -> Int64
requirementBytes t Text
values = [Int64] -> Int64
forall a (f :: * -> *). (Foldable f, Num a) => f a -> a
sum [Int64
32 Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Text -> Int64
nativeTextBytes Text
clause | Text
clause <- t Text -> [Text]
forall a. t a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList t Text
values]
         in Int64
256
                Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Int64
nameBytes
                Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ [Int64] -> Int64
forall a (f :: * -> *). (Foldable f, Num a) => f a -> a
sum ((Text -> Int64) -> [Text] -> [Int64]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Int64
nativeTextBytes [Text]
strings)
                Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
BS.length (GemRow -> ByteString
grSource GemRow
row))
                Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ GemRequirement -> Int64
forall {t :: * -> *}. Foldable t => t Text -> Int64
requirementBytes (GemRow -> GemRequirement
grRequiredRuby GemRow
row)
                Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ GemRequirement -> Int64
forall {t :: * -> *}. Foldable t => t Text -> Int64
requirementBytes (GemRow -> GemRequirement
grRequiredRubyGems GemRow
row)
                Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ [Int64] -> Int64
forall a (f :: * -> *). (Foldable f, Num a) => f a -> a
sum [Int64
64 Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Text -> Int64
nativeTextBytes (PackageName -> Text
renderPackageName (GemDependency -> PackageName
gdName GemDependency
dependency)) Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ GemRequirement -> Int64
forall {t :: * -> *}. Foldable t => t Text -> Int64
requirementBytes (GemDependency -> GemRequirement
gdRequirement GemDependency
dependency) | GemDependency
dependency <- GemRow -> [GemDependency]
grDependencies GemRow
row]
                Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ [Int64] -> Int64
forall a (f :: * -> *). (Foldable f, Num a) => f a -> a
sum [Int64
64 Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Text -> Int64
nativeTextBytes Text
key Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Text -> Int64
nativeTextBytes Text
value | (Text
key, Text
value) <- Map Text Text -> [(Text, Text)]
forall k a. Map k a -> [(k, a)]
Map.toList (GemRow -> Map Text Text
grExtras GemRow
row)]

nativeTextBytes :: Text -> Int64
nativeTextBytes :: Text -> Int64
nativeTextBytes = Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Int64) -> (Text -> Int) -> Text -> Int64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Int
textStorageBytes