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

-- | Listing refusal text, with a compact form that discards per-entry diagnostics.
module Ecluse.Core.Server.Response.Summary (
    detailedRejections,
    summarisedRejections,
) where

import Data.Map.Strict qualified as Map
import Data.Text qualified as T

import Ecluse.Core.Server.Response (
    RejectReason (..),
    Rejection (..),
    RuleName (..),
    ServeDecision (..),
 )

-- | Join full rejection messages for responses that keep per-entry diagnostics.
detailedRejections :: [ServeDecision] -> Text
detailedRejections :: [ServeDecision] -> Text
detailedRejections [ServeDecision]
decisions = case (ServeDecision -> Maybe Text) -> [ServeDecision] -> [Text]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe ServeDecision -> Maybe Text
rejectionText [ServeDecision]
decisions of
    [] -> Text
emptyListing
    [Text]
reasons -> Text -> [Text] -> Text
T.intercalate Text
"; " [Text]
reasons
  where
    rejectionText :: ServeDecision -> Maybe Text
rejectionText ServeDecision
Admit = Maybe Text
forall a. Maybe a
Nothing
    rejectionText (Reject Rejection
rejection) = Text -> Maybe Text
forall a. a -> Maybe a
Just (Rejection -> Text
rejectionMessage Rejection
rejection)

-- | Count rejection entries by typed cause, without reading per-version messages or policy reasons.
summarisedRejections :: [ServeDecision] -> Text
summarisedRejections :: [ServeDecision] -> Text
summarisedRejections [ServeDecision]
decisions
    | Map Cause Int -> Bool
forall k a. Map k a -> Bool
Map.null Map Cause Int
counts = Text
emptyListing
    | Bool
otherwise = Text -> [Text] -> Text
T.intercalate Text
". " (((Cause, Int) -> Text) -> [(Cause, Int)] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (Cause, Int) -> Text
forall {a}. Show a => (Cause, a) -> Text
renderGroup (Map Cause Int -> [(Cause, Int)]
forall k a. Map k a -> [(k, a)]
Map.toAscList Map Cause Int
counts))
  where
    counts :: Map Cause Int
counts = (Map Cause Int -> ServeDecision -> Map Cause Int)
-> Map Cause Int -> [ServeDecision] -> Map Cause Int
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Map Cause Int -> ServeDecision -> Map Cause Int
countDecision Map Cause Int
forall k a. Map k a
Map.empty [ServeDecision]
decisions
    countDecision :: Map Cause Int -> ServeDecision -> Map Cause Int
countDecision Map Cause Int
acc ServeDecision
Admit = Map Cause Int
acc
    countDecision Map Cause Int
acc (Reject Rejection
rejection) = (Int -> Int -> Int)
-> Cause -> Int -> Map Cause Int -> Map Cause Int
forall k a. Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
Map.insertWith Int -> Int -> Int
forall a. Num a => a -> a -> a
(+) (RejectReason -> Cause
causeOf (Rejection -> RejectReason
rejectionReason Rejection
rejection)) (Int
1 :: Int) Map Cause Int
acc
    renderGroup :: (Cause, a) -> Text
renderGroup (Cause
cause, a
count) = Cause -> Text
causeText Cause
cause Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" (rejection entries: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> a -> Text
forall b a. (Show a, IsString b) => a -> b
show a
count Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")"

data Cause
    = Policy RuleName
    | IntegrityMissing
    | IntegrityBelowFloor
    | VettingUnavailable
    | InvalidUpstream
    deriving stock (Cause -> Cause -> Bool
(Cause -> Cause -> Bool) -> (Cause -> Cause -> Bool) -> Eq Cause
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Cause -> Cause -> Bool
== :: Cause -> Cause -> Bool
$c/= :: Cause -> Cause -> Bool
/= :: Cause -> Cause -> Bool
Eq, Eq Cause
Eq Cause =>
(Cause -> Cause -> Ordering)
-> (Cause -> Cause -> Bool)
-> (Cause -> Cause -> Bool)
-> (Cause -> Cause -> Bool)
-> (Cause -> Cause -> Bool)
-> (Cause -> Cause -> Cause)
-> (Cause -> Cause -> Cause)
-> Ord Cause
Cause -> Cause -> Bool
Cause -> Cause -> Ordering
Cause -> Cause -> Cause
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Cause -> Cause -> Ordering
compare :: Cause -> Cause -> Ordering
$c< :: Cause -> Cause -> Bool
< :: Cause -> Cause -> Bool
$c<= :: Cause -> Cause -> Bool
<= :: Cause -> Cause -> Bool
$c> :: Cause -> Cause -> Bool
> :: Cause -> Cause -> Bool
$c>= :: Cause -> Cause -> Bool
>= :: Cause -> Cause -> Bool
$cmax :: Cause -> Cause -> Cause
max :: Cause -> Cause -> Cause
$cmin :: Cause -> Cause -> Cause
min :: Cause -> Cause -> Cause
Ord)

causeOf :: RejectReason -> Cause
causeOf :: RejectReason -> Cause
causeOf = \case
    ByPolicy (RuleName Text
name) [Reason]
_ -> RuleName -> Cause
Policy (Text -> RuleName
RuleName (Text -> Text
T.copy Text
name))
    RejectReason
MissingIntegrity -> Cause
IntegrityMissing
    RejectReason
BelowIntegrityFloor -> Cause
IntegrityBelowFloor
    Unavailable{} -> Cause
VettingUnavailable
    RejectReason
UpstreamInvalid -> Cause
InvalidUpstream

causeText :: Cause -> Text
causeText :: Cause -> Text
causeText = \case
    Policy (RuleName Text
name) -> Text
name Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
": " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
policyExplanation Text
name
    Cause
IntegrityMissing -> Text
"MissingIntegrity: artifacts lack an integrity digest"
    Cause
IntegrityBelowFloor -> Text
"BelowIntegrityFloor: artifact digests fall below the configured integrity floor"
    Cause
VettingUnavailable -> Text
"Unavailable: versions could not be vetted"
    Cause
InvalidUpstream -> Text
"UpstreamInvalid: an upstream document names a different package"

policyExplanation :: Text -> Text
policyExplanation :: Text -> Text
policyExplanation = \case
    Text
"BlockedByDefault" -> Text
"no allow rule matched"
    Text
"DenyInstallTimeExecution" -> Text
"installation runs code"
    Text
"DenyExternalDependencies" -> Text
"dependencies install from outside the registry"
    Text
"DenyIfKnownMalicious" -> Text
"known malicious advisories affect the versions"
    Text
"DenyIfCve" -> Text
"advisories deny admission under the configured severity policy"
    Text
"DenyIfEpss" -> Text
"advisories deny admission under the configured exploit probability policy"
    Text
"DenyIfYanked" -> Text
"the versions are yanked"
    Text
"DenyIfDeprecated" -> Text
"the versions are deprecated"
    Text
"DenyByIdentity" -> Text
"the operator revoked the package identity"
    Text
_ -> Text
"the policy rule denied admission"

emptyListing :: Text
emptyListing :: Text
emptyListing = Text
"no versions are available for this package"