from __future__ import annotations

from collections.abc import Sequence
from dataclasses import dataclass
from typing import Literal, Protocol, runtime_checkable


IDENTITY_MODULE_ID = "identity"
CAPABILITY_IDENTITY_DIRECTORY = "identity.directory"
CAPABILITY_IDENTITY_SEARCH = "identity.search"

IdentityStatus = Literal["active", "inactive", "suspended"]


@dataclass(frozen=True, slots=True)
class IdentityRef:
    id: str
    display_name: str | None = None
    external_subject: str | None = None
    source: str = "local"
    primary_account_id: str | None = None
    account_ids: tuple[str, ...] = ()
    status: IdentityStatus = "active"


@dataclass(frozen=True, slots=True)
class IdentityAccountLinkRef:
    id: str
    identity_id: str
    account_id: str
    is_primary: bool = False
    source: str = "local"


@runtime_checkable
class IdentityDirectory(Protocol):
    def get_identity(self, identity_id: str) -> IdentityRef | None:
        ...

    def identity_for_account(self, account_id: str) -> IdentityRef | None:
        ...

    def identities_for_accounts(self, account_ids: Sequence[str]) -> Sequence[IdentityRef]:
        ...

    def accounts_for_identity(self, identity_id: str) -> Sequence[IdentityAccountLinkRef]:
        ...


@runtime_checkable
class IdentitySearchProvider(Protocol):
    def search_identities(
        self,
        query: str | None = None,
        *,
        include_inactive: bool = False,
        limit: int = 25,
    ) -> Sequence[IdentityRef]:
        ...
