-- SPDX-FileCopyrightText: 2026 Alexandra de Wit
--
-- SPDX-License-Identifier: MIT
{-# LANGUAGE RoleAnnotations #-}

{- | First-party declarations by exact name or separator-delimited prefix, compared through one
ownership key. Each adapter declares how it reads and matches its entries, so a mount's
'FirstParty' names no ecosystem. Prefix grammars share 'FirstPartyGrammar', and its tag keeps one
ecosystem's entries apart from another's.
-}
module Ecluse.Core.Registry.FirstParty (
    -- * Adapter declarations
    FirstPartyNames (..),
    FirstPartyDeclaration (..),
    FirstParty (..),
    firstPartyName,

    -- * Shared name and prefix grammar
    FirstPartyGrammar (..),
    FirstPartyEntry (..),
    FirstPartyPrefix,
    parseFirstPartyEntry,
    ownsFirstPartyName,
) where

import Data.Text qualified as T
import Data.Text.Short (ShortText)
import Data.Text.Short qualified as TS
import Data.Typeable (cast)
import Text.Show (showParen, showString, showsPrec)

import Ecluse.Core.Package (PackageName)
import Ecluse.Core.Registry (ParseError, parseRefusal)

-- | How an adapter reads its configured @firstParty@ entries and matches a name against them.
data FirstPartyNames entry = FirstPartyNames
    { forall entry.
FirstPartyNames entry -> Text -> Either ParseError entry
namesEntry :: Text -> Either ParseError entry
    -- ^ Read one configured entry.
    , forall entry.
FirstPartyNames entry -> NonEmpty entry -> PackageName -> Bool
namesOwn :: NonEmpty entry -> PackageName -> Bool
    -- ^ Whether the declared entries own a name. Deny by default.
    , forall entry. FirstPartyNames entry -> Text
namesEntryNoun :: Text
    -- ^ What a refused entry is called, as in @invalid scope in firstParty@.
    , forall entry. FirstPartyNames entry -> Text
namesListNoun :: Text
    -- ^ What a declaration must name at least one of.
    }

-- | An adapter's first-party grammar, whatever its entry type.
data FirstPartyDeclaration = forall entry. (Eq entry, Show entry, Typeable entry) => FirstPartyDeclaration (FirstPartyNames entry)

-- | A mount's declared entries, under the grammar of the adapter that read them.
data FirstParty = forall entry. (Eq entry, Show entry, Typeable entry) => FirstParty (FirstPartyNames entry) (NonEmpty entry)

-- | Declarations compare by their entries. Entries of another type, such as another adapter's, never compare equal.
instance Eq FirstParty where
    FirstParty FirstPartyNames entry
_ NonEmpty entry
left == :: FirstParty -> FirstParty -> Bool
== FirstParty FirstPartyNames entry
_ NonEmpty entry
right = NonEmpty entry -> Maybe (NonEmpty entry)
forall a b. (Typeable a, Typeable b) => a -> Maybe b
cast NonEmpty entry
right Maybe (NonEmpty entry) -> Maybe (NonEmpty entry) -> Bool
forall a. Eq a => a -> a -> Bool
== NonEmpty entry -> Maybe (NonEmpty entry)
forall a. a -> Maybe a
Just NonEmpty entry
left

instance Show FirstParty where
    showsPrec :: Int -> FirstParty -> ShowS
showsPrec Int
precedence (FirstParty FirstPartyNames entry
_ NonEmpty entry
entries) = Bool -> ShowS -> ShowS
showParen (Int
precedence Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
10) (String -> ShowS
showString String
"FirstParty " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> NonEmpty entry -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 NonEmpty entry
entries)

-- | Whether the mount's declaration owns a name: the one predicate every consumer of the privilege reads.
firstPartyName :: FirstParty -> PackageName -> Bool
firstPartyName :: FirstParty -> PackageName -> Bool
firstPartyName (FirstParty FirstPartyNames entry
names NonEmpty entry
entries) = FirstPartyNames entry -> NonEmpty entry -> PackageName -> Bool
forall entry.
FirstPartyNames entry -> NonEmpty entry -> PackageName -> Bool
namesOwn FirstPartyNames entry
names NonEmpty entry
entries

-- | How one ecosystem spells and compares first-party names.
data FirstPartyGrammar ecosystem = FirstPartyGrammar
    { forall {k} (ecosystem :: k). FirstPartyGrammar ecosystem -> Text
fpLabel :: Text
    -- ^ The ecosystem's name in a refusal.
    , forall {k} (ecosystem :: k).
FirstPartyGrammar ecosystem -> Char -> Bool
fpSeparator :: Char -> Bool
    -- ^ The characters a prefix must end with before its @*@.
    , forall {k} (ecosystem :: k).
FirstPartyGrammar ecosystem
-> Text -> Either ParseError PackageName
fpProjectName :: Text -> Either ParseError PackageName
    -- ^ The native name parser, applied to exact names and to prefixes with their separator.
    , forall {k} (ecosystem :: k).
FirstPartyGrammar ecosystem -> PackageName -> Maybe Text
fpOwnershipKey :: PackageName -> Maybe Text
    -- ^ The text ownership compares. 'Nothing' for a name the ecosystem never owns.
    }

-- RoleAnnotations is not in GHC2021. Without these lines the tag takes GHC's phantom role, and
-- coerce would move one ecosystem's entries under another's grammar.
type role FirstPartyGrammar nominal

-- | An ownership-key prefix with a validated terminal separator.
newtype FirstPartyPrefix = FirstPartyPrefix ShortText
    deriving stock (FirstPartyPrefix -> FirstPartyPrefix -> Bool
(FirstPartyPrefix -> FirstPartyPrefix -> Bool)
-> (FirstPartyPrefix -> FirstPartyPrefix -> Bool)
-> Eq FirstPartyPrefix
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: FirstPartyPrefix -> FirstPartyPrefix -> Bool
== :: FirstPartyPrefix -> FirstPartyPrefix -> Bool
$c/= :: FirstPartyPrefix -> FirstPartyPrefix -> Bool
/= :: FirstPartyPrefix -> FirstPartyPrefix -> Bool
Eq, Int -> FirstPartyPrefix -> ShowS
[FirstPartyPrefix] -> ShowS
FirstPartyPrefix -> String
(Int -> FirstPartyPrefix -> ShowS)
-> (FirstPartyPrefix -> String)
-> ([FirstPartyPrefix] -> ShowS)
-> Show FirstPartyPrefix
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> FirstPartyPrefix -> ShowS
showsPrec :: Int -> FirstPartyPrefix -> ShowS
$cshow :: FirstPartyPrefix -> String
show :: FirstPartyPrefix -> String
$cshowList :: [FirstPartyPrefix] -> ShowS
showList :: [FirstPartyPrefix] -> ShowS
Show)

-- | An exact name or a prefix declared by the registry operator.
data FirstPartyEntry ecosystem
    = -- | Claim the names whose ownership key equals this name's.
      OwnedName PackageName
    | -- | Claim the names whose ownership key begins with this prefix, separator included.
      OwnedPrefix FirstPartyPrefix
    deriving stock (FirstPartyEntry ecosystem -> FirstPartyEntry ecosystem -> Bool
(FirstPartyEntry ecosystem -> FirstPartyEntry ecosystem -> Bool)
-> (FirstPartyEntry ecosystem -> FirstPartyEntry ecosystem -> Bool)
-> Eq (FirstPartyEntry ecosystem)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall k (ecosystem :: k).
FirstPartyEntry ecosystem -> FirstPartyEntry ecosystem -> Bool
$c== :: forall k (ecosystem :: k).
FirstPartyEntry ecosystem -> FirstPartyEntry ecosystem -> Bool
== :: FirstPartyEntry ecosystem -> FirstPartyEntry ecosystem -> Bool
$c/= :: forall k (ecosystem :: k).
FirstPartyEntry ecosystem -> FirstPartyEntry ecosystem -> Bool
/= :: FirstPartyEntry ecosystem -> FirstPartyEntry ecosystem -> Bool
Eq, Int -> FirstPartyEntry ecosystem -> ShowS
[FirstPartyEntry ecosystem] -> ShowS
FirstPartyEntry ecosystem -> String
(Int -> FirstPartyEntry ecosystem -> ShowS)
-> (FirstPartyEntry ecosystem -> String)
-> ([FirstPartyEntry ecosystem] -> ShowS)
-> Show (FirstPartyEntry ecosystem)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall k (ecosystem :: k).
Int -> FirstPartyEntry ecosystem -> ShowS
forall k (ecosystem :: k). [FirstPartyEntry ecosystem] -> ShowS
forall k (ecosystem :: k). FirstPartyEntry ecosystem -> String
$cshowsPrec :: forall k (ecosystem :: k).
Int -> FirstPartyEntry ecosystem -> ShowS
showsPrec :: Int -> FirstPartyEntry ecosystem -> ShowS
$cshow :: forall k (ecosystem :: k). FirstPartyEntry ecosystem -> String
show :: FirstPartyEntry ecosystem -> String
$cshowList :: forall k (ecosystem :: k). [FirstPartyEntry ecosystem] -> ShowS
showList :: [FirstPartyEntry ecosystem] -> ShowS
Show)

type role FirstPartyEntry nominal

-- | Parse an exact name or a prefix ending in a separator then @*@. Refuse @acme*@, which would claim @acmeco@.
parseFirstPartyEntry :: FirstPartyGrammar ecosystem -> Text -> Either ParseError (FirstPartyEntry ecosystem)
parseFirstPartyEntry :: forall {k} (ecosystem :: k).
FirstPartyGrammar ecosystem
-> Text -> Either ParseError (FirstPartyEntry ecosystem)
parseFirstPartyEntry FirstPartyGrammar ecosystem
grammar Text
entry = case Text -> Text -> Maybe Text
T.stripSuffix Text
"*" Text
entry of
    Just Text
prefix | Text -> Bool
endsAtSeparator Text
prefix -> do
        name <- FirstPartyGrammar ecosystem
-> Text -> Either ParseError PackageName
forall {k} (ecosystem :: k).
FirstPartyGrammar ecosystem
-> Text -> Either ParseError PackageName
fpProjectName FirstPartyGrammar ecosystem
grammar Text
prefix
        key <- maybe invalid Right (fpOwnershipKey grammar name)
        pure (OwnedPrefix (FirstPartyPrefix (TS.fromText key)))
    Just Text
_ -> Either ParseError (FirstPartyEntry ecosystem)
forall a. Either ParseError a
invalid
    Maybe Text
Nothing -> PackageName -> FirstPartyEntry ecosystem
forall {k} (ecosystem :: k).
PackageName -> FirstPartyEntry ecosystem
OwnedName (PackageName -> FirstPartyEntry ecosystem)
-> Either ParseError PackageName
-> Either ParseError (FirstPartyEntry ecosystem)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FirstPartyGrammar ecosystem
-> Text -> Either ParseError PackageName
forall {k} (ecosystem :: k).
FirstPartyGrammar ecosystem
-> Text -> Either ParseError PackageName
fpProjectName FirstPartyGrammar ecosystem
grammar Text
entry
  where
    invalid :: Either ParseError a
    invalid :: forall a. Either ParseError a
invalid = Text -> Either ParseError a
forall a. Text -> Either ParseError a
parseRefusal (Text
"invalid " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> FirstPartyGrammar ecosystem -> Text
forall {k} (ecosystem :: k). FirstPartyGrammar ecosystem -> Text
fpLabel FirstPartyGrammar ecosystem
grammar Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" first-party entry: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
forall b a. (Show a, IsString b) => a -> b
show Text
entry)
    endsAtSeparator :: Text -> Bool
endsAtSeparator = Bool -> ((Text, Char) -> Bool) -> Maybe (Text, Char) -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (FirstPartyGrammar ecosystem -> Char -> Bool
forall {k} (ecosystem :: k).
FirstPartyGrammar ecosystem -> Char -> Bool
fpSeparator FirstPartyGrammar ecosystem
grammar (Char -> Bool) -> ((Text, Char) -> Char) -> (Text, Char) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text, Char) -> Char
forall a b. (a, b) -> b
snd) (Maybe (Text, Char) -> Bool)
-> (Text -> Maybe (Text, Char)) -> Text -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Maybe (Text, Char)
T.unsnoc

-- | Whether any entry owns the name. A name without an ownership key is never owned.
ownsFirstPartyName :: FirstPartyGrammar ecosystem -> NonEmpty (FirstPartyEntry ecosystem) -> PackageName -> Bool
ownsFirstPartyName :: forall {k} (ecosystem :: k).
FirstPartyGrammar ecosystem
-> NonEmpty (FirstPartyEntry ecosystem) -> PackageName -> Bool
ownsFirstPartyName FirstPartyGrammar ecosystem
grammar NonEmpty (FirstPartyEntry ecosystem)
entries PackageName
name = Bool -> (Text -> Bool) -> Maybe Text -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (\Text
key -> (FirstPartyEntry ecosystem -> Bool)
-> NonEmpty (FirstPartyEntry ecosystem) -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (Text -> FirstPartyEntry ecosystem -> Bool
forall {k} {ecosystem :: k}.
Text -> FirstPartyEntry ecosystem -> Bool
owns Text
key) NonEmpty (FirstPartyEntry ecosystem)
entries) (FirstPartyGrammar ecosystem -> PackageName -> Maybe Text
forall {k} (ecosystem :: k).
FirstPartyGrammar ecosystem -> PackageName -> Maybe Text
fpOwnershipKey FirstPartyGrammar ecosystem
grammar PackageName
name)
  where
    owns :: Text -> FirstPartyEntry ecosystem -> Bool
owns Text
key = \case
        OwnedName PackageName
owned -> FirstPartyGrammar ecosystem -> PackageName -> Maybe Text
forall {k} (ecosystem :: k).
FirstPartyGrammar ecosystem -> PackageName -> Maybe Text
fpOwnershipKey FirstPartyGrammar ecosystem
grammar PackageName
owned Maybe Text -> Maybe Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text -> Maybe Text
forall a. a -> Maybe a
Just Text
key
        OwnedPrefix (FirstPartyPrefix ShortText
prefix) -> ShortText -> Text
TS.toText ShortText
prefix Text -> Text -> Bool
`T.isPrefixOf` Text
key