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

-- | Public HTTP attempt kinds and raw outcomes shared by observation and telemetry.
module Ecluse.Core.Telemetry.Upstream (
    PublicReadKind (..),
    publicReadKindName,
    ExchangeOutcome (..),
    exchangeOutcomeName,
) where

-- | The public operation that caused an HTTP attempt.
data PublicReadKind = Listing | SelectedRead | Artifact | Companion | WorkerMetadata | WorkerArtifact
    deriving stock (PublicReadKind -> PublicReadKind -> Bool
(PublicReadKind -> PublicReadKind -> Bool)
-> (PublicReadKind -> PublicReadKind -> Bool) -> Eq PublicReadKind
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PublicReadKind -> PublicReadKind -> Bool
== :: PublicReadKind -> PublicReadKind -> Bool
$c/= :: PublicReadKind -> PublicReadKind -> Bool
/= :: PublicReadKind -> PublicReadKind -> Bool
Eq, Int -> PublicReadKind -> ShowS
[PublicReadKind] -> ShowS
PublicReadKind -> String
(Int -> PublicReadKind -> ShowS)
-> (PublicReadKind -> String)
-> ([PublicReadKind] -> ShowS)
-> Show PublicReadKind
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PublicReadKind -> ShowS
showsPrec :: Int -> PublicReadKind -> ShowS
$cshow :: PublicReadKind -> String
show :: PublicReadKind -> String
$cshowList :: [PublicReadKind] -> ShowS
showList :: [PublicReadKind] -> ShowS
Show)

-- | The bounded wire name of a public request kind.
publicReadKindName :: PublicReadKind -> Text
publicReadKindName :: PublicReadKind -> Text
publicReadKindName = \case
    PublicReadKind
Listing -> Text
"listing"
    PublicReadKind
SelectedRead -> Text
"selected_read"
    PublicReadKind
Artifact -> Text
"artifact"
    PublicReadKind
Companion -> Text
"companion"
    PublicReadKind
WorkerMetadata -> Text
"worker_metadata"
    PublicReadKind
WorkerArtifact -> Text
"worker_artifact"

-- | A response status or an interruption before response headers arrive.
data ExchangeOutcome = ResponseStatus Int | TransportFailure | Cancelled
    deriving stock (ExchangeOutcome -> ExchangeOutcome -> Bool
(ExchangeOutcome -> ExchangeOutcome -> Bool)
-> (ExchangeOutcome -> ExchangeOutcome -> Bool)
-> Eq ExchangeOutcome
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ExchangeOutcome -> ExchangeOutcome -> Bool
== :: ExchangeOutcome -> ExchangeOutcome -> Bool
$c/= :: ExchangeOutcome -> ExchangeOutcome -> Bool
/= :: ExchangeOutcome -> ExchangeOutcome -> Bool
Eq, Int -> ExchangeOutcome -> ShowS
[ExchangeOutcome] -> ShowS
ExchangeOutcome -> String
(Int -> ExchangeOutcome -> ShowS)
-> (ExchangeOutcome -> String)
-> ([ExchangeOutcome] -> ShowS)
-> Show ExchangeOutcome
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ExchangeOutcome -> ShowS
showsPrec :: Int -> ExchangeOutcome -> ShowS
$cshow :: ExchangeOutcome -> String
show :: ExchangeOutcome -> String
$cshowList :: [ExchangeOutcome] -> ShowS
showList :: [ExchangeOutcome] -> ShowS
Show)

-- | Keep exact HTTP codes distinct from transport failure and cancellation.
exchangeOutcomeName :: ExchangeOutcome -> Text
exchangeOutcomeName :: ExchangeOutcome -> Text
exchangeOutcomeName = \case
    ResponseStatus Int
code -> Int -> Text
forall b a. (Show a, IsString b) => a -> b
show Int
code
    ExchangeOutcome
TransportFailure -> Text
"transport_failure"
    ExchangeOutcome
Cancelled -> Text
"cancelled"