src/lightspeed@integration@adapter_sdk.erl

-module(lightspeed@integration@adapter_sdk).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/lightspeed/integration/adapter_sdk.gleam").
-export([sdk_version/0, compatibility_policy/0, postgres_cdc_source_adapter/0, object_store_source_adapter/0, queue_source_adapter/0, billing_service_adapter/0, identity_service_adapter/0, reference_adapters/0, valid/1, compatible/2, reference_adapters_compatible/1, semver_label/1, boundary_label/1, capability_label/1, verification_step_signature/1, verification_signature/1, adapter_signature/1, compatibility_policy_label/1, compatibility_signature/1, name/1, boundary/1, capabilities/1, verification_steps/1]).
-export_type([semver/0, boundary/0, capability/0, verification_step/0, adapter_contract/0, compatibility_policy/0]).

-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.

?MODULEDOC(" Ecosystem adapter SDK contracts and reference adapters for M41.\n").

-type semver() :: {semver, integer(), integer(), integer()}.

-type boundary() :: database_source |
    object_store_source |
    queue_source |
    billing_service |
    identity_service.

-type capability() :: pull_batch |
    push_batch |
    ack_checkpoint |
    replay_checkpoint |
    idempotent_writes |
    webhook_verification |
    tenant_scoped_auth |
    health_probe.

-type verification_step() :: {verification_step, binary(), binary(), binary()}.

-type adapter_contract() :: {adapter_contract,
        binary(),
        boundary(),
        binary(),
        semver(),
        semver(),
        list(capability()),
        list(verification_step()),
        binary(),
        binary()}.

-type compatibility_policy() :: {compatibility_policy,
        integer(),
        integer(),
        integer(),
        integer(),
        integer()}.

-file("src/lightspeed/integration/adapter_sdk.gleam", 64).
?DOC(" Current stable SDK version.\n").
-spec sdk_version() -> semver().
sdk_version() ->
    {semver, 1, 0, 0}.

-file("src/lightspeed/integration/adapter_sdk.gleam", 69).
?DOC(" Current adapter compatibility policy.\n").
-spec compatibility_policy() -> compatibility_policy().
compatibility_policy() ->
    {compatibility_policy, 1, 1, 0, 4, 180}.

-file("src/lightspeed/integration/adapter_sdk.gleam", 338).
-spec default_verification_steps(binary()) -> list(verification_step()).
default_verification_steps(Prefix) ->
    [{verification_step,
            <<Prefix/binary, "_contract_handshake"/utf8>>,
            <<"Verify adapter handshake against SDK contract schema."/utf8>>,
            <<<<"evidence."/utf8, Prefix/binary>>/binary,
                ".contract_handshake"/utf8>>},
        {verification_step,
            <<Prefix/binary, "_version_compatibility"/utf8>>,
            <<"Verify adapter version within supported policy window."/utf8>>,
            <<<<"evidence."/utf8, Prefix/binary>>/binary,
                ".version_compatibility"/utf8>>},
        {verification_step,
            <<Prefix/binary, "_round_trip_replay"/utf8>>,
            <<"Run deterministic round-trip and replay verification."/utf8>>,
            <<<<"evidence."/utf8, Prefix/binary>>/binary,
                ".round_trip_replay"/utf8>>},
        {verification_step,
            <<Prefix/binary, "_operator_handoff"/utf8>>,
            <<"Verify operator handoff runbook and escalation hooks."/utf8>>,
            <<<<"evidence."/utf8, Prefix/binary>>/binary,
                ".operator_handoff"/utf8>>}].

-file("src/lightspeed/integration/adapter_sdk.gleam", 80).
?DOC(" Reference adapter: Postgres CDC source.\n").
-spec postgres_cdc_source_adapter() -> adapter_contract().
postgres_cdc_source_adapter() ->
    {adapter_contract,
        <<"postgres_cdc_source"/utf8>>,
        database_source,
        <<"Lightspeed.Adapter.PostgresCdc"/utf8>>,
        sdk_version(),
        {semver, 1, 2, 0},
        [pull_batch,
            ack_checkpoint,
            replay_checkpoint,
            tenant_scoped_auth,
            health_probe],
        default_verification_steps(<<"postgres_cdc_source"/utf8>>),
        <<"ga"/utf8>>,
        <<"docs/ecosystem_adapter_sdk.md#postgres-cdc-source"/utf8>>}.

-file("src/lightspeed/integration/adapter_sdk.gleam", 101).
?DOC(" Reference adapter: object-store batch source.\n").
-spec object_store_source_adapter() -> adapter_contract().
object_store_source_adapter() ->
    {adapter_contract,
        <<"object_store_source"/utf8>>,
        object_store_source,
        <<"Lightspeed.Adapter.ObjectStoreSource"/utf8>>,
        sdk_version(),
        {semver, 1, 1, 3},
        [pull_batch,
            ack_checkpoint,
            replay_checkpoint,
            tenant_scoped_auth,
            health_probe],
        default_verification_steps(<<"object_store_source"/utf8>>),
        <<"ga"/utf8>>,
        <<"docs/ecosystem_adapter_sdk.md#object-store-source"/utf8>>}.

-file("src/lightspeed/integration/adapter_sdk.gleam", 122).
?DOC(" Reference adapter: queue source.\n").
-spec queue_source_adapter() -> adapter_contract().
queue_source_adapter() ->
    {adapter_contract,
        <<"queue_source"/utf8>>,
        queue_source,
        <<"Lightspeed.Adapter.QueueSource"/utf8>>,
        sdk_version(),
        {semver, 1, 3, 1},
        [pull_batch,
            push_batch,
            ack_checkpoint,
            replay_checkpoint,
            tenant_scoped_auth,
            health_probe],
        default_verification_steps(<<"queue_source"/utf8>>),
        <<"ga"/utf8>>,
        <<"docs/ecosystem_adapter_sdk.md#queue-source"/utf8>>}.

-file("src/lightspeed/integration/adapter_sdk.gleam", 144).
?DOC(" Reference adapter: billing service.\n").
-spec billing_service_adapter() -> adapter_contract().
billing_service_adapter() ->
    {adapter_contract,
        <<"billing_service"/utf8>>,
        billing_service,
        <<"Lightspeed.Adapter.BillingService"/utf8>>,
        sdk_version(),
        {semver, 1, 0, 5},
        [push_batch,
            idempotent_writes,
            webhook_verification,
            tenant_scoped_auth,
            health_probe],
        default_verification_steps(<<"billing_service"/utf8>>),
        <<"ga"/utf8>>,
        <<"docs/ecosystem_adapter_sdk.md#billing-service"/utf8>>}.

-file("src/lightspeed/integration/adapter_sdk.gleam", 165).
?DOC(" Reference adapter: identity provider.\n").
-spec identity_service_adapter() -> adapter_contract().
identity_service_adapter() ->
    {adapter_contract,
        <<"identity_service"/utf8>>,
        identity_service,
        <<"Lightspeed.Adapter.IdentityService"/utf8>>,
        sdk_version(),
        {semver, 1, 4, 0},
        [pull_batch,
            push_batch,
            idempotent_writes,
            tenant_scoped_auth,
            health_probe],
        default_verification_steps(<<"identity_service"/utf8>>),
        <<"ga"/utf8>>,
        <<"docs/ecosystem_adapter_sdk.md#identity-service"/utf8>>}.

-file("src/lightspeed/integration/adapter_sdk.gleam", 186).
?DOC(" Reference M41 adapters.\n").
-spec reference_adapters() -> list(adapter_contract()).
reference_adapters() ->
    [postgres_cdc_source_adapter(),
        object_store_source_adapter(),
        queue_source_adapter(),
        billing_service_adapter(),
        identity_service_adapter()].

-file("src/lightspeed/integration/adapter_sdk.gleam", 397).
-spec verification_steps_entries_valid(list(verification_step())) -> boolean().
verification_steps_entries_valid(Steps) ->
    case Steps of
        [] ->
            true;

        [Step | Rest] ->
            (((erlang:element(2, Step) /= <<""/utf8>>) andalso (erlang:element(
                3,
                Step
            )
            /= <<""/utf8>>))
            andalso (erlang:element(4, Step) /= <<""/utf8>>))
            andalso verification_steps_entries_valid(Rest)
    end.

-file("src/lightspeed/integration/adapter_sdk.gleam", 393).
-spec verification_steps_valid(list(verification_step())) -> boolean().
verification_steps_valid(Steps) ->
    (erlang:length(Steps) >= 4) andalso verification_steps_entries_valid(Steps).

-file("src/lightspeed/integration/adapter_sdk.gleam", 382).
-spec contains_capability(list(capability()), capability()) -> boolean().
contains_capability(Capabilities, Expected) ->
    case Capabilities of
        [] ->
            false;

        [Capability | Rest] ->
            (Capability =:= Expected) orelse contains_capability(Rest, Expected)
    end.

-file("src/lightspeed/integration/adapter_sdk.gleam", 378).
-spec requires_tenant_scoped_auth(list(capability())) -> boolean().
requires_tenant_scoped_auth(Capabilities) ->
    contains_capability(Capabilities, tenant_scoped_auth).

-file("src/lightspeed/integration/adapter_sdk.gleam", 374).
-spec requires_health_probe(list(capability())) -> boolean().
requires_health_probe(Capabilities) ->
    contains_capability(Capabilities, health_probe).

-file("src/lightspeed/integration/adapter_sdk.gleam", 367).
-spec capabilities_non_empty(list(capability())) -> boolean().
capabilities_non_empty(Capabilities) ->
    case Capabilities of
        [] ->
            false;

        [_ | _] ->
            true
    end.

-file("src/lightspeed/integration/adapter_sdk.gleam", 363).
-spec semver_valid(semver()) -> boolean().
semver_valid(Version) ->
    ((erlang:element(2, Version) >= 0) andalso (erlang:element(3, Version) >= 0))
    andalso (erlang:element(4, Version) >= 0).

-file("src/lightspeed/integration/adapter_sdk.gleam", 197).
?DOC(" Validate one adapter contract.\n").
-spec valid(adapter_contract()) -> boolean().
valid(Adapter) ->
    ((((((((((erlang:element(2, Adapter) /= <<""/utf8>>) andalso (erlang:element(
        4,
        Adapter
    )
    /= <<""/utf8>>))
    andalso (erlang:element(9, Adapter) =:= <<"ga"/utf8>>))
    andalso (erlang:element(10, Adapter) /= <<""/utf8>>))
    andalso semver_valid(erlang:element(5, Adapter)))
    andalso semver_valid(erlang:element(6, Adapter)))
    andalso (erlang:length(erlang:element(7, Adapter)) >= 4))
    andalso capabilities_non_empty(erlang:element(7, Adapter)))
    andalso requires_health_probe(erlang:element(7, Adapter)))
    andalso requires_tenant_scoped_auth(erlang:element(7, Adapter)))
    andalso verification_steps_valid(erlang:element(8, Adapter)).

-file("src/lightspeed/integration/adapter_sdk.gleam", 212).
?DOC(" Validate adapter compatibility against one policy.\n").
-spec compatible(adapter_contract(), compatibility_policy()) -> boolean().
compatible(Adapter, Policy) ->
    ((((erlang:element(2, erlang:element(5, Adapter)) =:= erlang:element(
        2,
        Policy
    ))
    andalso (erlang:element(2, erlang:element(6, Adapter)) =:= erlang:element(
        3,
        Policy
    )))
    andalso (erlang:element(3, erlang:element(6, Adapter)) >= erlang:element(
        4,
        Policy
    )))
    andalso (erlang:element(3, erlang:element(6, Adapter)) =< erlang:element(
        5,
        Policy
    )))
    andalso (erlang:element(6, Policy) >= 90).

-file("src/lightspeed/integration/adapter_sdk.gleam", 408).
-spec all_adapters_valid_and_compatible(
    list(adapter_contract()),
    compatibility_policy()
) -> boolean().
all_adapters_valid_and_compatible(Adapters, Policy) ->
    case Adapters of
        [] ->
            true;

        [Adapter | Rest] ->
            (valid(Adapter) andalso compatible(Adapter, Policy)) andalso all_adapters_valid_and_compatible(
                Rest,
                Policy
            )
    end.

-file("src/lightspeed/integration/adapter_sdk.gleam", 224).
?DOC(" Validate all reference adapters against one policy.\n").
-spec reference_adapters_compatible(compatibility_policy()) -> boolean().
reference_adapters_compatible(Policy) ->
    _pipe = reference_adapters(),
    all_adapters_valid_and_compatible(_pipe, Policy).

-file("src/lightspeed/integration/adapter_sdk.gleam", 230).
?DOC(" Stable semver label.\n").
-spec semver_label(semver()) -> binary().
semver_label(Version) ->
    <<<<<<<<(erlang:integer_to_binary(erlang:element(2, Version)))/binary,
                    "."/utf8>>/binary,
                (erlang:integer_to_binary(erlang:element(3, Version)))/binary>>/binary,
            "."/utf8>>/binary,
        (erlang:integer_to_binary(erlang:element(4, Version)))/binary>>.

-file("src/lightspeed/integration/adapter_sdk.gleam", 239).
?DOC(" Stable boundary label.\n").
-spec boundary_label(boundary()) -> binary().
boundary_label(Boundary) ->
    case Boundary of
        database_source ->
            <<"database_source"/utf8>>;

        object_store_source ->
            <<"object_store_source"/utf8>>;

        queue_source ->
            <<"queue_source"/utf8>>;

        billing_service ->
            <<"billing_service"/utf8>>;

        identity_service ->
            <<"identity_service"/utf8>>
    end.

-file("src/lightspeed/integration/adapter_sdk.gleam", 250).
?DOC(" Stable capability label.\n").
-spec capability_label(capability()) -> binary().
capability_label(Capability) ->
    case Capability of
        pull_batch ->
            <<"pull_batch"/utf8>>;

        push_batch ->
            <<"push_batch"/utf8>>;

        ack_checkpoint ->
            <<"ack_checkpoint"/utf8>>;

        replay_checkpoint ->
            <<"replay_checkpoint"/utf8>>;

        idempotent_writes ->
            <<"idempotent_writes"/utf8>>;

        webhook_verification ->
            <<"webhook_verification"/utf8>>;

        tenant_scoped_auth ->
            <<"tenant_scoped_auth"/utf8>>;

        health_probe ->
            <<"health_probe"/utf8>>
    end.

-file("src/lightspeed/integration/adapter_sdk.gleam", 314).
?DOC(" Stable verification step signature.\n").
-spec verification_step_signature(verification_step()) -> binary().
verification_step_signature(Step) ->
    <<<<(erlang:element(2, Step))/binary, ":evidence="/utf8>>/binary,
        (erlang:element(4, Step))/binary>>.

-file("src/lightspeed/integration/adapter_sdk.gleam", 421).
-spec join_with(binary(), list(binary())) -> binary().
join_with(Separator, Values) ->
    case Values of
        [] ->
            <<""/utf8>>;

        [Value] ->
            Value;

        [Value@1 | Rest] ->
            <<<<Value@1/binary, Separator/binary>>/binary,
                (join_with(Separator, Rest))/binary>>
    end.

-file("src/lightspeed/integration/adapter_sdk.gleam", 309).
?DOC(" Stable verification signature.\n").
-spec verification_signature(list(verification_step())) -> binary().
verification_signature(Steps) ->
    join_with(
        <<";"/utf8>>,
        gleam@list:map(Steps, fun verification_step_signature/1)
    ).

-file("src/lightspeed/integration/adapter_sdk.gleam", 264).
?DOC(" Stable adapter signature.\n").
-spec adapter_signature(adapter_contract()) -> binary().
adapter_signature(Adapter) ->
    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"adapter:"/utf8,
                                                                (erlang:element(
                                                                    2,
                                                                    Adapter
                                                                ))/binary>>/binary,
                                                            "|boundary="/utf8>>/binary,
                                                        (boundary_label(
                                                            erlang:element(
                                                                3,
                                                                Adapter
                                                            )
                                                        ))/binary>>/binary,
                                                    "|provider="/utf8>>/binary,
                                                (erlang:element(4, Adapter))/binary>>/binary,
                                            "|sdk="/utf8>>/binary,
                                        (semver_label(
                                            erlang:element(5, Adapter)
                                        ))/binary>>/binary,
                                    "|adapter_version="/utf8>>/binary,
                                (semver_label(erlang:element(6, Adapter)))/binary>>/binary,
                            "|capabilities="/utf8>>/binary,
                        (join_with(
                            <<","/utf8>>,
                            gleam@list:map(
                                erlang:element(7, Adapter),
                                fun capability_label/1
                            )
                        ))/binary>>/binary,
                    "|support_tier="/utf8>>/binary,
                (erlang:element(9, Adapter))/binary>>/binary,
            "|verification="/utf8>>/binary,
        (verification_signature(erlang:element(8, Adapter)))/binary>>.

-file("src/lightspeed/integration/adapter_sdk.gleam", 284).
?DOC(" Stable compatibility policy label.\n").
-spec compatibility_policy_label(compatibility_policy()) -> binary().
compatibility_policy_label(Policy) ->
    <<<<<<<<<<<<<<<<<<"sdk_major="/utf8,
                                        (erlang:integer_to_binary(
                                            erlang:element(2, Policy)
                                        ))/binary>>/binary,
                                    "|adapter_major="/utf8>>/binary,
                                (erlang:integer_to_binary(
                                    erlang:element(3, Policy)
                                ))/binary>>/binary,
                            "|min_minor="/utf8>>/binary,
                        (erlang:integer_to_binary(erlang:element(4, Policy)))/binary>>/binary,
                    "|max_minor="/utf8>>/binary,
                (erlang:integer_to_binary(erlang:element(5, Policy)))/binary>>/binary,
            "|deprecation_window_days="/utf8>>/binary,
        (erlang:integer_to_binary(erlang:element(6, Policy)))/binary>>.

-file("src/lightspeed/integration/adapter_sdk.gleam", 429).
-spec bool_label(boolean()) -> binary().
bool_label(Value) ->
    case Value of
        true ->
            <<"true"/utf8>>;

        false ->
            <<"false"/utf8>>
    end.

-file("src/lightspeed/integration/adapter_sdk.gleam", 298).
?DOC(" Stable compatibility signature across all reference adapters.\n").
-spec compatibility_signature(compatibility_policy()) -> binary().
compatibility_signature(Policy) ->
    Entries = begin
        _pipe = reference_adapters(),
        gleam@list:map(
            _pipe,
            fun(Adapter) ->
                <<<<(erlang:element(2, Adapter))/binary, ":compatible="/utf8>>/binary,
                    (bool_label(compatible(Adapter, Policy)))/binary>>
            end
        )
    end,
    <<<<(compatibility_policy_label(Policy))/binary, "|matrix="/utf8>>/binary,
        (join_with(<<","/utf8>>, Entries))/binary>>.

-file("src/lightspeed/integration/adapter_sdk.gleam", 319).
?DOC(" Adapter name accessor.\n").
-spec name(adapter_contract()) -> binary().
name(Adapter) ->
    erlang:element(2, Adapter).

-file("src/lightspeed/integration/adapter_sdk.gleam", 324).
?DOC(" Adapter boundary accessor.\n").
-spec boundary(adapter_contract()) -> boundary().
boundary(Adapter) ->
    erlang:element(3, Adapter).

-file("src/lightspeed/integration/adapter_sdk.gleam", 329).
?DOC(" Adapter capability accessor.\n").
-spec capabilities(adapter_contract()) -> list(capability()).
capabilities(Adapter) ->
    erlang:element(7, Adapter).

-file("src/lightspeed/integration/adapter_sdk.gleam", 334).
?DOC(" Adapter verification steps accessor.\n").
-spec verification_steps(adapter_contract()) -> list(verification_step()).
verification_steps(Adapter) ->
    erlang:element(8, Adapter).