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

-- | Field charges shared by selected releases and durable mirror records. Aliases count per occurrence.
module Ecluse.Core.Server.Cache.VersionWeight.Fields (rawWeight, retainedRawWeight, detailsFieldsWeight, nameWeight, textWeight, itemsWeight, artifactWeight, weighEntryKey) where

import Data.Map.Strict qualified as Map
import Data.Text.Short qualified as TS
import Data.Text.Unsafe (lengthWord8)
import Data.Time (UTCTime (..), diffTimeToPicoseconds, toModifiedJulianDay)

import Ecluse.Core.Package
import Ecluse.Core.Package.Entry (EntryKey (..))
import Ecluse.Core.Registry.CachedDocument (CachedDoc, weighCachedDoc)
import Ecluse.Core.Server.MemoryModel (expandWireBytes, expandWireBytesInteger)
import Ecluse.Core.Text (textStorageBytes)
import Ecluse.Core.Version (renderVersion)

-- | Charge compact source size on the shared expansion without encoding.
rawWeight :: CachedDoc -> Integer
rawWeight :: CachedDoc -> Integer
rawWeight = Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int -> Integer) -> (CachedDoc -> Int) -> CachedDoc -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int
expandWireBytes (Int -> Int) -> (CachedDoc -> Int) -> CachedDoc -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int64 -> Int) -> (CachedDoc -> Int64) -> CachedDoc -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CachedDoc -> Int64
weighCachedDoc

-- | State retention uses Integer expansion. Selected entries keep their existing Int arithmetic.
retainedRawWeight :: CachedDoc -> Integer
retainedRawWeight :: CachedDoc -> Integer
retainedRawWeight = Integer -> Integer
expandWireBytesInteger (Integer -> Integer)
-> (CachedDoc -> Integer) -> CachedDoc -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Int64 -> Integer) -> (CachedDoc -> Int64) -> CachedDoc -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CachedDoc -> Int64
weighCachedDoc

-- | Preserve variable release charges independently of the selected-entry shell.
detailsFieldsWeight :: PackageDetails -> Integer
detailsFieldsWeight :: PackageDetails -> Integer
detailsFieldsWeight PackageDetails
details =
    PackageName -> Integer
nameWeight (PackageDetails -> PackageName
pkgName PackageDetails
details)
        Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Text -> Integer
textWeight Text
rawVersion
        Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
256 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
rawLength
        Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer -> (UTCTime -> Integer) -> Maybe UTCTime -> Integer
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Integer
0 UTCTime -> Integer
timeWeight (PackageDetails -> Maybe UTCTime
pkgPublishedAt PackageDetails
details)
        Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ CodeExecSignal -> Integer
installWeight (PackageDetails -> CodeExecSignal
pkgInstallCode PackageDetails
details)
        Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ DependencySources -> Integer
dependencyWeight (PackageDetails -> DependencySources
pkgDependencySources PackageDetails
details)
        Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ (Artifact -> Integer) -> NonEmpty Artifact -> Integer
forall (f :: * -> *) a.
Foldable f =>
(a -> Integer) -> f a -> Integer
itemsWeight Artifact -> Integer
artifactWeight (PackageDetails -> NonEmpty Artifact
pkgArtifacts PackageDetails
details)
  where
    -- Parsed tokens and bounded numeric components use this allowance, which also covers RubyGems hyphen expansion and copied parser text.
    rawVersion :: Text
rawVersion = Version -> Text
renderVersion (PackageDetails -> Version
pkgVersion PackageDetails
details)
    rawLength :: Int
rawLength = Text -> Int
lengthWord8 Text
rawVersion

-- | Charge all package spellings, including aliases.
nameWeight :: PackageName -> Integer
nameWeight :: PackageName -> Integer
nameWeight PackageName
name =
    [Integer] -> Integer
forall a (f :: * -> *). (Foldable f, Num a) => f a -> a
sum ((ShortText -> Integer) -> [ShortText] -> [Integer]
forall a b. (a -> b) -> [a] -> [b]
map (Text -> Integer
textWeight (Text -> Integer) -> (ShortText -> Text) -> ShortText -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortText -> Text
TS.toText) [PackageName -> ShortText
pkgCanonical PackageName
name, PackageName -> ShortText
pkgBaseName PackageName
name])
        Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Text -> Integer
textWeight (PackageName -> Text
renderPackageName PackageName
name)
        Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer -> (Scope -> Integer) -> Maybe Scope -> Integer
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Integer
0 (Text -> Integer
textWeight (Text -> Integer) -> (Scope -> Text) -> Scope -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Scope -> Text
renderScope) (PackageName -> Maybe Scope
pkgNamespace PackageName
name)

-- Decimal digits overestimate Integer payload bytes without depending on its heap layout.
timeWeight :: UTCTime -> Integer
timeWeight :: UTCTime -> Integer
timeWeight (UTCTime Day
day DiffTime
time) =
    Text -> Integer
textWeight (Integer -> Text
forall b a. (Show a, IsString b) => a -> b
show (Day -> Integer
toModifiedJulianDay Day
day)) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Text -> Integer
textWeight (Integer -> Text
forall b a. (Show a, IsString b) => a -> b
show (DiffTime -> Integer
diffTimeToPicoseconds DiffTime
time))

-- | Count the complete backing allocation of each text occurrence.
textWeight :: Text -> Integer
textWeight :: Text -> Integer
textWeight Text
text = Integer
128 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Text -> Int
textStorageBytes Text
text)

-- | Include a list or map element allowance for each occurrence.
itemsWeight :: (Foldable f) => (a -> Integer) -> f a -> Integer
itemsWeight :: forall (f :: * -> *) a.
Foldable f =>
(a -> Integer) -> f a -> Integer
itemsWeight a -> Integer
weigh = (Integer -> a -> Integer) -> Integer -> f a -> Integer
forall b a. (b -> a -> b) -> b -> f a -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\Integer
total a
value -> Integer
total Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
256 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ a -> Integer
weigh a
value) Integer
0

installWeight :: CodeExecSignal -> Integer
installWeight :: CodeExecSignal -> Integer
installWeight = \case
    CodeExecSignal
NoCodeOnInstall -> Integer
0
    RunsCodeOnInstall Text
reason -> Text -> Integer
textWeight Text
reason
    CodeExecSignal
CodeExecUnknown -> Integer
0

dependencyWeight :: DependencySources -> Integer
dependencyWeight :: DependencySources -> Integer
dependencyWeight = \case
    DependencySources
RegistryDependencies -> Integer
0
    FetchesOutsideRegistry Text
reason -> Text -> Integer
textWeight Text
reason
    DependencySources
DependencySourcesUnknown -> Integer
0

-- | Charge coordinates, source strings, digests and companion declarations.
artifactWeight :: Artifact -> Integer
artifactWeight :: Artifact -> Integer
artifactWeight Artifact
artifact =
    EntryKey -> Integer
weighEntryKey (Artifact -> EntryKey
artEntryKey Artifact
artifact)
        Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Text -> Integer
textWeight (Artifact -> Text
artFilename Artifact
artifact)
        Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Text -> Integer
textWeight (Artifact -> Text
artUrl Artifact
artifact)
        Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ (Hash -> Integer) -> [Hash] -> Integer
forall (f :: * -> *) a.
Foldable f =>
(a -> Integer) -> f a -> Integer
itemsWeight (Text -> Integer
textWeight (Text -> Integer) -> (Hash -> Text) -> Hash -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Hash -> Text
hashValue) (Artifact -> [Hash]
artHashes Artifact
artifact)
        Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
-> (CoreMetadata -> Integer) -> Maybe CoreMetadata -> Integer
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Integer
0 CoreMetadata -> Integer
metadataWeight (Artifact -> Maybe CoreMetadata
artCoreMetadata Artifact
artifact)

metadataWeight :: CoreMetadata -> Integer
metadataWeight :: CoreMetadata -> Integer
metadataWeight (MetadataAvailable Bool
_) = Integer
64
metadataWeight (MetadataDigests Map Text Text
digests) = Integer
64 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ ((Text, Text) -> Integer) -> [(Text, Text)] -> Integer
forall (f :: * -> *) a.
Foldable f =>
(a -> Integer) -> f a -> Integer
itemsWeight (\(Text
name, Text
value) -> Text -> Integer
textWeight Text
name Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Text -> Integer
textWeight Text
value) (Map Text Text -> [(Text, Text)]
forall k a. Map k a -> [(k, a)]
Map.toList Map Text Text
digests)

-- | Charge an entry coordinate, including any retained text backing allocation.
weighEntryKey :: EntryKey -> Integer
weighEntryKey :: EntryKey -> Integer
weighEntryKey = \case
    ArrayEntry Int
_ -> Integer
64
    RecordEntry Int
_ -> Integer
64
    ObjectEntry Text
key -> Integer
64 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Text -> Integer
textWeight Text
key
    EntryKey
SingletonEntry -> Integer
16