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

-- | RubyGems requests use checked native coordinates and the selected origin's authority.
module Ecluse.Core.Registry.RubyGems.Request (
    gemInfoRequestUnder,
    gemDirectoryRequestUnder,
    gemInfoUrl,
    gemArtifactUrl,
    gemCompactSpecUrl,
    gemPath,
    compactSpecPath,
) where

import Network.HTTP.Client (Request)

import Ecluse.Core.Credential (ClientCredential)
import Ecluse.Core.Package (PackageName, renderPackageName)
import Ecluse.Core.Registry (UrlFormationError (UnparseableUrl))
import Ecluse.Core.Registry.Request (BaseRequest, joinPath, metadataGet, requestAt)
import Ecluse.Core.Registry.RubyGems.Credential (rubyReadCredential)
import Ecluse.Core.Registry.RubyGems.Project (GemCoordinate, coordinateFilename, coordinateStem, projectName, unscopedRubyGems)
import Ecluse.Core.Server.Path (encodeComponent, unFilename)

-- | Fetch the named compact-index document under caller-selected credentials.
gemInfoRequestUnder :: BaseRequest -> Maybe ClientCredential -> PackageName -> Either UrlFormationError Request
gemInfoRequestUnder :: BaseRequest
-> Maybe ClientCredential
-> PackageName
-> Either UrlFormationError Request
gemInfoRequestUnder BaseRequest
base Maybe ClientCredential
credential PackageName
name = do
    path <- PackageName -> Either UrlFormationError Text
infoPath PackageName
name
    metadataGet rubyReadCredential credential "text/plain" <$> requestAt base path

-- | Construct the named upstream path without acquiring a registry-wide catalogue.
gemInfoUrl :: Text -> PackageName -> Either UrlFormationError Text
gemInfoUrl :: Text -> PackageName -> Either UrlFormationError Text
gemInfoUrl Text
base PackageName
name = PackageName -> Either UrlFormationError Text
infoPath PackageName
name Either UrlFormationError Text
-> (Text -> Either UrlFormationError Text)
-> Either UrlFormationError Text
forall a b.
Either UrlFormationError a
-> (a -> Either UrlFormationError b) -> Either UrlFormationError b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Text -> Text -> Either UrlFormationError Text
joinPath Text
base

infoPath :: PackageName -> Either UrlFormationError Text
infoPath :: PackageName -> Either UrlFormationError Text
infoPath PackageName
name = (Text
"info/" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>) (Text -> Text) -> (Text -> Text) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
encodeComponent (Text -> Text)
-> Either UrlFormationError Text -> Either UrlFormationError Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> PackageName -> Either UrlFormationError Text
nativeName PackageName
name

nativeName :: PackageName -> Either UrlFormationError Text
nativeName :: PackageName -> Either UrlFormationError Text
nativeName PackageName
name = do
    Bool -> Either UrlFormationError () -> Either UrlFormationError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (PackageName -> Bool
unscopedRubyGems PackageName
name) (UrlFormationError -> Either UrlFormationError ()
forall a b. a -> Either a b
Left (Text -> UrlFormationError
UnparseableUrl Text
"not an unscoped RubyGems package"))
    checked <- (ParseError -> UrlFormationError)
-> Either ParseError PackageName
-> Either UrlFormationError PackageName
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (UrlFormationError -> ParseError -> UrlFormationError
forall a b. a -> b -> a
const (Text -> UrlFormationError
UnparseableUrl Text
"invalid RubyGems package name")) (Text -> Either ParseError PackageName
projectName (PackageName -> Text
renderPackageName PackageName
name))
    pure (renderPackageName checked)

-- | Address an artifact by its exact checked native filename.
gemArtifactUrl :: Text -> GemCoordinate -> Either UrlFormationError Text
gemArtifactUrl :: Text -> GemCoordinate -> Either UrlFormationError Text
gemArtifactUrl Text
base GemCoordinate
coordinate = Text -> Text -> Either UrlFormationError Text
joinPath Text
base (Text -> Text
gemPath (Filename -> Text
unFilename (GemCoordinate -> Filename
coordinateFilename GemCoordinate
coordinate)))

-- | The path of an archive by its native filename, under the registry root.
gemPath :: Text -> Text
gemPath :: Text -> Text
gemPath Text
filename = Text
"gems/" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
encodeComponent Text
filename

-- | Address the original zlib-compressed compact specification for one checked artifact.
gemCompactSpecUrl :: Text -> GemCoordinate -> Either UrlFormationError Text
gemCompactSpecUrl :: Text -> GemCoordinate -> Either UrlFormationError Text
gemCompactSpecUrl Text
base GemCoordinate
coordinate = Text -> Text -> Either UrlFormationError Text
joinPath Text
base (Text -> Text
compactSpecPath (GemCoordinate -> Text
coordinateStem GemCoordinate
coordinate))

-- | The path of a compact specification by its archive's filename stem, under the registry root.
compactSpecPath :: Text -> Text
compactSpecPath :: Text -> Text
compactSpecPath Text
stem = Text
"quick/Marshal.4.8/" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
encodeComponent Text
stem Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
".gemspec.rz"

-- | Directory reads retain the selected caller or anonymous public authority.
gemDirectoryRequestUnder :: BaseRequest -> Maybe ClientCredential -> Either UrlFormationError Request
gemDirectoryRequestUnder :: BaseRequest
-> Maybe ClientCredential -> Either UrlFormationError Request
gemDirectoryRequestUnder BaseRequest
base Maybe ClientCredential
credential = CredentialMapping
-> Maybe ClientCredential -> ByteString -> Request -> Request
metadataGet CredentialMapping
rubyReadCredential Maybe ClientCredential
credential ByteString
"text/plain" (Request -> Request)
-> Either UrlFormationError Request
-> Either UrlFormationError Request
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> BaseRequest -> Text -> Either UrlFormationError Request
requestAt BaseRequest
base Text
"versions"