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

{- | A durable mirror format's operations over its managed documents.
Each adapter declares them, so the mirror view restores and assembles retained versions without naming a format.
-}
module Ecluse.Core.Mirror.Documents (
    MirrorDocuments (..),
    restoreFragments,
) where

import Data.Map.Strict qualified as Map
import Data.Traversable (for)

import Ecluse.Core.Mirror.State (MirrorVersion, ProjectMirrorRecord, fragmentDetails, fragmentReadData, versionFragments)
import Ecluse.Core.Package (Artifact (..), PackageDetails (pkgArtifacts))
import Ecluse.Core.Package.Entry (EntryKey)
import Ecluse.Core.Registry.CachedDocument (CachedDoc)
import Ecluse.Core.Registry.Metadata (VersionDoc)
import Ecluse.Core.Registry.MirrorUpload (MirrorReadData)

-- | Restore, ordering and assembly of one format's managed mirror documents.
data MirrorDocuments = MirrorDocuments
    { MirrorDocuments -> MirrorVersion -> [Artifact] -> Maybe VersionDoc
mdRestore :: MirrorVersion -> [Artifact] -> Maybe VersionDoc
    -- ^ Pair retained source entries with the current target files. Any missing pairing restores nothing.
    , MirrorDocuments -> [(Text, VersionDoc)] -> [(Text, VersionDoc)]
mdOrder :: [(Text, VersionDoc)] -> [(Text, VersionDoc)]
    -- ^ Place restored versions, supplied in version-text order, in the native listing order.
    , MirrorDocuments -> Int -> EntryKey
mdEntryKey :: Int -> EntryKey
    -- ^ The native entry coordinate for a project-wide position.
    , MirrorDocuments
-> (EntryKey -> EntryKey) -> CachedDoc -> Maybe CachedDoc
mdRekey :: (EntryKey -> EntryKey) -> CachedDoc -> Maybe CachedDoc
    -- ^ Rewrite a restored document's entry coordinates.
    , MirrorDocuments -> ProjectMirrorRecord -> [VersionDoc] -> CachedDoc
mdAssemble :: ProjectMirrorRecord -> [VersionDoc] -> CachedDoc
    -- ^ Assemble the managed listing from rebased versions in listing order.
    }

-- | Pair each fragment's retained entries with the current target file of the same name, numbering artifacts across fragments.
restoreFragments ::
    (Int -> EntryKey) ->
    (MirrorReadData -> Maybe (source, Map EntryKey entry)) ->
    MirrorVersion ->
    [Artifact] ->
    Maybe (NonEmpty (PackageDetails, source, NonEmpty (Artifact, entry)))
restoreFragments :: forall source entry.
(Int -> EntryKey)
-> (MirrorReadData -> Maybe (source, Map EntryKey entry))
-> MirrorVersion
-> [Artifact]
-> Maybe
     (NonEmpty (PackageDetails, source, NonEmpty (Artifact, entry)))
restoreFragments Int -> EntryKey
entryKey MirrorReadData -> Maybe (source, Map EntryKey entry)
project MirrorVersion
mirror [Artifact]
current = NonEmpty
  (Maybe (PackageDetails, source, NonEmpty (Artifact, entry)))
-> Maybe
     (NonEmpty (PackageDetails, source, NonEmpty (Artifact, entry)))
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
forall (f :: * -> *) a.
Applicative f =>
NonEmpty (f a) -> f (NonEmpty a)
sequenceA NonEmpty
  (Maybe (PackageDetails, source, NonEmpty (Artifact, entry)))
restored
  where
    target :: Map Text Artifact
target = [(Text, Artifact)] -> Map Text Artifact
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(Artifact -> Text
artFilename Artifact
artifact, Artifact
artifact) | Artifact
artifact <- [Artifact]
current]
    (Int
_, NonEmpty
  (Maybe (PackageDetails, source, NonEmpty (Artifact, entry)))
restored) = (Int
 -> StoredMirrorFragment
 -> (Int,
     Maybe (PackageDetails, source, NonEmpty (Artifact, entry))))
-> Int
-> NonEmpty StoredMirrorFragment
-> (Int,
    NonEmpty
      (Maybe (PackageDetails, source, NonEmpty (Artifact, entry))))
forall (t :: * -> *) s a b.
Traversable t =>
(s -> a -> (s, b)) -> s -> t a -> (s, t b)
mapAccumL (\Int
start StoredMirrorFragment
fragment -> (Int
start Int -> Int -> Int
forall a. Num a => a -> a -> a
+ NonEmpty Artifact -> Int
forall a. NonEmpty a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (PackageDetails -> NonEmpty Artifact
pkgArtifacts (StoredMirrorFragment -> PackageDetails
fragmentDetails StoredMirrorFragment
fragment)), Int
-> StoredMirrorFragment
-> Maybe (PackageDetails, source, NonEmpty (Artifact, entry))
restoreFragment Int
start StoredMirrorFragment
fragment)) Int
0 (MirrorVersion -> NonEmpty StoredMirrorFragment
versionFragments MirrorVersion
mirror)
    restoreFragment :: Int
-> StoredMirrorFragment
-> Maybe (PackageDetails, source, NonEmpty (Artifact, entry))
restoreFragment Int
start StoredMirrorFragment
fragment = do
        (source, entries) <- MirrorReadData -> Maybe (source, Map EntryKey entry)
project (StoredMirrorFragment -> MirrorReadData
fragmentReadData StoredMirrorFragment
fragment)
        let original = StoredMirrorFragment -> PackageDetails
fragmentDetails StoredMirrorFragment
fragment
        pairs <- for (zip [start ..] (toList (pkgArtifacts original))) $ \(Int
position, Artifact
artifact) -> do
            native <- Text -> Map Text Artifact -> Maybe Artifact
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup (Artifact -> Text
artFilename Artifact
artifact) Map Text Artifact
target
            entry <- Map.lookup (artEntryKey artifact) entries
            pure (artifact{artEntryKey = entryKey position, artUrl = artUrl native}, entry)
        (original,source,) <$> nonEmpty pairs