src/lightspeed@platform@convention_profile.erl

-module(lightspeed@platform@convention_profile).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/lightspeed/platform/convention_profile.gleam").
-export([default_profile/0, websocket_fallback_hatch/0, data_partition_hatch/0, template_diagnostics_hatch/0, escape_hatch/4, with_escape_hatch/2, name/1, escape_hatches/1, hidden_runtime_behavior/1, has_stable_override_seams/1, valid/1, quick_start_ready/1, configuration_surface/1, configuration_surface_size/1, configuration_surface_signature/1, override_seam_signature/1, signature/1, reference_profiles/0, fixture_snapshots/0, snapshot_signature/0]).
-export_type([runtime_convention/0, data_convention/0, template_convention/0, escape_seam/0, escape_hatch/0, core_profile/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(" Convention-over-configuration core profile contracts for M45.\n").

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

-type data_convention() :: {data_convention,
        binary(),
        binary(),
        binary(),
        boolean()}.

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

-type escape_seam() :: runtime_seam | data_seam | template_seam.

-type escape_hatch() :: {escape_hatch,
        escape_seam(),
        binary(),
        binary(),
        binary()}.

-type core_profile() :: {core_profile,
        binary(),
        runtime_convention(),
        data_convention(),
        template_convention(),
        list(escape_hatch()),
        boolean()}.

-file("src/lightspeed/platform/convention_profile.gleam", 64).
?DOC(" Narrow M45 default profile for common runtime/data/template workflows.\n").
-spec default_profile() -> core_profile().
default_profile() ->
    {core_profile,
        <<"core_default"/utf8>>,
        {runtime_convention,
            <<"MyApp.Web.Endpoint"/utf8>>,
            <<"MyApp.Web.Router"/utf8>>,
            <<"websocket_primary"/utf8>>,
            <<"MyApp.Session.Store"/utf8>>,
            <<"explicit_supervision_plan"/utf8>>},
        {data_convention,
            <<"MyApp.Data.Scope"/utf8>>,
            <<"MyApp.Data.Repository"/utf8>>,
            <<"sql_migrations"/utf8>>,
            true},
        {template_convention,
            <<"MyApp.Web.TemplateErgonomics"/utf8>>,
            <<"MyApp.Web.TemplateCompiler"/utf8>>,
            <<"explicit_slot_defaults"/utf8>>,
            <<"strict_deterministic"/utf8>>},
        [],
        false}.

-file("src/lightspeed/platform/convention_profile.gleam", 92).
?DOC(" Predefined runtime seam escape hatch.\n").
-spec websocket_fallback_hatch() -> escape_hatch().
websocket_fallback_hatch() ->
    {escape_hatch,
        runtime_seam,
        <<"MyApp.Runtime.TransportHooks"/utf8>>,
        <<"override_transport_fallback"/utf8>>,
        <<"enable websocket+sse fallback profile for constrained networks"/utf8>>}.

-file("src/lightspeed/platform/convention_profile.gleam", 102).
?DOC(" Predefined data seam escape hatch.\n").
-spec data_partition_hatch() -> escape_hatch().
data_partition_hatch() ->
    {escape_hatch,
        data_seam,
        <<"MyApp.Data.PartitionHooks"/utf8>>,
        <<"resolve_partition_for_tenant"/utf8>>,
        <<"route selected tenants to non-default shard topology"/utf8>>}.

-file("src/lightspeed/platform/convention_profile.gleam", 112).
?DOC(" Predefined template seam escape hatch.\n").
-spec template_diagnostics_hatch() -> escape_hatch().
template_diagnostics_hatch() ->
    {escape_hatch,
        template_seam,
        <<"MyApp.Web.TemplateHooks"/utf8>>,
        <<"customize_diagnostic_hints"/utf8>>,
        <<"add domain-specific migration hints for legacy templates"/utf8>>}.

-file("src/lightspeed/platform/convention_profile.gleam", 122).
?DOC(" Build one explicit escape hatch.\n").
-spec escape_hatch(escape_seam(), binary(), binary(), binary()) -> escape_hatch().
escape_hatch(Seam, Module, Hook, Rationale) ->
    {escape_hatch, Seam, Module, Hook, Rationale}.

-file("src/lightspeed/platform/convention_profile.gleam", 351).
-spec append_hatch(list(escape_hatch()), escape_hatch()) -> list(escape_hatch()).
append_hatch(Hatches, Hatch) ->
    case Hatches of
        [] ->
            [Hatch];

        [Entry | Rest] ->
            [Entry | append_hatch(Rest, Hatch)]
    end.

-file("src/lightspeed/platform/convention_profile.gleam", 372).
-spec seam_label(escape_seam()) -> binary().
seam_label(Seam) ->
    case Seam of
        runtime_seam ->
            <<"runtime"/utf8>>;

        data_seam ->
            <<"data"/utf8>>;

        template_seam ->
            <<"template"/utf8>>
    end.

-file("src/lightspeed/platform/convention_profile.gleam", 337).
-spec hatch_contract_key(escape_hatch()) -> binary().
hatch_contract_key(Hatch) ->
    <<<<<<<<(seam_label(erlang:element(2, Hatch)))/binary, ":"/utf8>>/binary,
                (erlang:element(3, Hatch))/binary>>/binary,
            ":"/utf8>>/binary,
        (erlang:element(4, Hatch))/binary>>.

-file("src/lightspeed/platform/convention_profile.gleam", 326).
-spec contains_hatch_contract(list(escape_hatch()), binary()) -> boolean().
contains_hatch_contract(Hatches, Key) ->
    case Hatches of
        [] ->
            false;

        [Hatch | Rest] ->
            case hatch_contract_key(Hatch) =:= Key of
                true ->
                    true;

                false ->
                    contains_hatch_contract(Rest, Key)
            end
    end.

-file("src/lightspeed/platform/convention_profile.gleam", 341).
-spec append_hatch_if_missing(list(escape_hatch()), escape_hatch()) -> list(escape_hatch()).
append_hatch_if_missing(Hatches, Hatch) ->
    case contains_hatch_contract(Hatches, hatch_contract_key(Hatch)) of
        true ->
            Hatches;

        false ->
            append_hatch(Hatches, Hatch)
    end.

-file("src/lightspeed/platform/convention_profile.gleam", 132).
?DOC(" Append one escape hatch when missing.\n").
-spec with_escape_hatch(core_profile(), escape_hatch()) -> core_profile().
with_escape_hatch(Profile, Hatch) ->
    {core_profile,
        erlang:element(2, Profile),
        erlang:element(3, Profile),
        erlang:element(4, Profile),
        erlang:element(5, Profile),
        append_hatch_if_missing(erlang:element(6, Profile), Hatch),
        erlang:element(7, Profile)}.

-file("src/lightspeed/platform/convention_profile.gleam", 143).
?DOC(" Profile name accessor.\n").
-spec name(core_profile()) -> binary().
name(Profile) ->
    erlang:element(2, Profile).

-file("src/lightspeed/platform/convention_profile.gleam", 148).
?DOC(" Escape-hatch accessor.\n").
-spec escape_hatches(core_profile()) -> list(escape_hatch()).
escape_hatches(Profile) ->
    erlang:element(6, Profile).

-file("src/lightspeed/platform/convention_profile.gleam", 153).
?DOC(" Hidden-runtime-behavior accessor.\n").
-spec hidden_runtime_behavior(core_profile()) -> boolean().
hidden_runtime_behavior(Profile) ->
    erlang:element(7, Profile).

-file("src/lightspeed/platform/convention_profile.gleam", 317).
-spec unique_hatch_contracts(list(escape_hatch())) -> boolean().
unique_hatch_contracts(Hatches) ->
    case Hatches of
        [] ->
            true;

        [Hatch | Rest] ->
            not contains_hatch_contract(Rest, hatch_contract_key(Hatch)) andalso unique_hatch_contracts(
                Rest
            )
    end.

-file("src/lightspeed/platform/convention_profile.gleam", 313).
-spec hatch_valid(escape_hatch()) -> boolean().
hatch_valid(Hatch) ->
    ((erlang:element(3, Hatch) /= <<""/utf8>>) andalso (erlang:element(4, Hatch)
    /= <<""/utf8>>))
    andalso (erlang:element(5, Hatch) /= <<""/utf8>>).

-file("src/lightspeed/platform/convention_profile.gleam", 306).
-spec hatches_valid(list(escape_hatch())) -> boolean().
hatches_valid(Hatches) ->
    case Hatches of
        [] ->
            true;

        [Hatch | Rest] ->
            hatch_valid(Hatch) andalso hatches_valid(Rest)
    end.

-file("src/lightspeed/platform/convention_profile.gleam", 178).
?DOC(" Stable override seam contract verification.\n").
-spec has_stable_override_seams(core_profile()) -> boolean().
has_stable_override_seams(Profile) ->
    hatches_valid(erlang:element(6, Profile)) andalso unique_hatch_contracts(
        erlang:element(6, Profile)
    ).

-file("src/lightspeed/platform/convention_profile.gleam", 299).
-spec template_valid(template_convention()) -> boolean().
template_valid(Template) ->
    (((erlang:element(2, Template) /= <<""/utf8>>) andalso (erlang:element(
        3,
        Template
    )
    /= <<""/utf8>>))
    andalso (erlang:element(4, Template) /= <<""/utf8>>))
    andalso (erlang:element(5, Template) /= <<""/utf8>>).

-file("src/lightspeed/platform/convention_profile.gleam", 292).
-spec data_valid(data_convention()) -> boolean().
data_valid(Data) ->
    (((erlang:element(2, Data) /= <<""/utf8>>) andalso (erlang:element(3, Data)
    /= <<""/utf8>>))
    andalso (erlang:element(4, Data) /= <<""/utf8>>))
    andalso erlang:element(5, Data).

-file("src/lightspeed/platform/convention_profile.gleam", 284).
-spec runtime_valid(runtime_convention()) -> boolean().
runtime_valid(Runtime) ->
    ((((erlang:element(2, Runtime) /= <<""/utf8>>) andalso (erlang:element(
        3,
        Runtime
    )
    /= <<""/utf8>>))
    andalso (erlang:element(4, Runtime) /= <<""/utf8>>))
    andalso (erlang:element(5, Runtime) /= <<""/utf8>>))
    andalso (erlang:element(6, Runtime) /= <<""/utf8>>).

-file("src/lightspeed/platform/convention_profile.gleam", 158).
?DOC(" Validate profile contract.\n").
-spec valid(core_profile()) -> boolean().
valid(Profile) ->
    (((((erlang:element(2, Profile) /= <<""/utf8>>) andalso runtime_valid(
        erlang:element(3, Profile)
    ))
    andalso data_valid(erlang:element(4, Profile)))
    andalso template_valid(erlang:element(5, Profile)))
    andalso hatches_valid(erlang:element(6, Profile)))
    andalso has_stable_override_seams(Profile).

-file("src/lightspeed/platform/convention_profile.gleam", 168).
?DOC(" Quick-start readiness for narrow default profile usage.\n").
-spec quick_start_ready(core_profile()) -> boolean().
quick_start_ready(Profile) ->
    ((((valid(Profile) andalso not erlang:element(7, Profile)) andalso (erlang:element(
        6,
        Profile
    )
    =:= []))
    andalso (erlang:element(6, erlang:element(3, Profile)) =:= <<"explicit_supervision_plan"/utf8>>))
    andalso erlang:element(5, erlang:element(4, Profile)))
    andalso (erlang:element(5, erlang:element(5, Profile)) =:= <<"strict_deterministic"/utf8>>).

-file("src/lightspeed/platform/convention_profile.gleam", 422).
-spec concat(list(binary()), list(binary())) -> list(binary()).
concat(Left, Right) ->
    case Left of
        [] ->
            Right;

        [Value | Rest] ->
            [Value | concat(Rest, Right)]
    end.

-file("src/lightspeed/platform/convention_profile.gleam", 184).
?DOC(" Flattened configuration surface for runtime/data/template workflows.\n").
-spec configuration_surface(core_profile()) -> list(binary()).
configuration_surface(Profile) ->
    Base = [<<"runtime.endpoint_module="/utf8,
            (erlang:element(2, erlang:element(3, Profile)))/binary>>,
        <<"runtime.router_module="/utf8,
            (erlang:element(3, erlang:element(3, Profile)))/binary>>,
        <<"runtime.transport_profile="/utf8,
            (erlang:element(4, erlang:element(3, Profile)))/binary>>,
        <<"data.scope_module="/utf8,
            (erlang:element(2, erlang:element(4, Profile)))/binary>>,
        <<"data.repository_module="/utf8,
            (erlang:element(3, erlang:element(4, Profile)))/binary>>,
        <<"template.ergonomics_module="/utf8,
            (erlang:element(2, erlang:element(5, Profile)))/binary>>],
    Hatch_entries = gleam@list:map(
        erlang:element(6, Profile),
        fun(Hatch) ->
            <<<<<<<<<<"escape_hatch."/utf8,
                                (seam_label(erlang:element(2, Hatch)))/binary>>/binary,
                            "="/utf8>>/binary,
                        (erlang:element(3, Hatch))/binary>>/binary,
                    "#"/utf8>>/binary,
                (erlang:element(4, Hatch))/binary>>
        end
    ),
    concat(Base, Hatch_entries).

-file("src/lightspeed/platform/convention_profile.gleam", 207).
?DOC(" Configuration surface size accessor.\n").
-spec configuration_surface_size(core_profile()) -> integer().
configuration_surface_size(Profile) ->
    erlang:length(configuration_surface(Profile)).

-file("src/lightspeed/platform/convention_profile.gleam", 429).
-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/platform/convention_profile.gleam", 212).
?DOC(" Stable configuration-surface signature.\n").
-spec configuration_surface_signature(core_profile()) -> binary().
configuration_surface_signature(Profile) ->
    <<"surface:"/utf8,
        (join_with(<<";"/utf8>>, configuration_surface(Profile)))/binary>>.

-file("src/lightspeed/platform/convention_profile.gleam", 361).
-spec seam_hooks(list(escape_hatch()), escape_seam()) -> list(binary()).
seam_hooks(Hatches, Seam) ->
    case Hatches of
        [] ->
            [];

        [Hatch | Rest] ->
            case erlang:element(2, Hatch) =:= Seam of
                true ->
                    [<<<<(erlang:element(3, Hatch))/binary, "#"/utf8>>/binary,
                            (erlang:element(4, Hatch))/binary>> |
                        seam_hooks(Rest, Seam)];

                false ->
                    seam_hooks(Rest, Seam)
            end
    end.

-file("src/lightspeed/platform/convention_profile.gleam", 217).
?DOC(" Stable override-seam signature.\n").
-spec override_seam_signature(core_profile()) -> binary().
override_seam_signature(Profile) ->
    <<<<<<<<<<"runtime="/utf8,
                        (join_with(
                            <<","/utf8>>,
                            seam_hooks(erlang:element(6, Profile), runtime_seam)
                        ))/binary>>/binary,
                    "|data="/utf8>>/binary,
                (join_with(
                    <<","/utf8>>,
                    seam_hooks(erlang:element(6, Profile), data_seam)
                ))/binary>>/binary,
            "|template="/utf8>>/binary,
        (join_with(
            <<","/utf8>>,
            seam_hooks(erlang:element(6, Profile), template_seam)
        ))/binary>>.

-file("src/lightspeed/platform/convention_profile.gleam", 415).
-spec bool_label(boolean()) -> binary().
bool_label(Value) ->
    case Value of
        true ->
            <<"true"/utf8>>;

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

-file("src/lightspeed/platform/convention_profile.gleam", 404).
-spec template_signature(template_convention()) -> binary().
template_signature(Template) ->
    <<<<<<<<<<<<<<"ergonomics="/utf8, (erlang:element(2, Template))/binary>>/binary,
                            "|compiler="/utf8>>/binary,
                        (erlang:element(3, Template))/binary>>/binary,
                    "|slot_defaults="/utf8>>/binary,
                (erlang:element(4, Template))/binary>>/binary,
            "|diagnostics="/utf8>>/binary,
        (erlang:element(5, Template))/binary>>.

-file("src/lightspeed/platform/convention_profile.gleam", 393).
-spec data_signature(data_convention()) -> binary().
data_signature(Data) ->
    <<<<<<<<<<<<<<"scope="/utf8, (erlang:element(2, Data))/binary>>/binary,
                            "|repository="/utf8>>/binary,
                        (erlang:element(3, Data))/binary>>/binary,
                    "|migration="/utf8>>/binary,
                (erlang:element(4, Data))/binary>>/binary,
            "|tenant_scope_required="/utf8>>/binary,
        (bool_label(erlang:element(5, Data)))/binary>>.

-file("src/lightspeed/platform/convention_profile.gleam", 380).
-spec runtime_signature(runtime_convention()) -> binary().
runtime_signature(Runtime) ->
    <<<<<<<<<<<<<<<<<<"endpoint="/utf8, (erlang:element(2, Runtime))/binary>>/binary,
                                    "|router="/utf8>>/binary,
                                (erlang:element(3, Runtime))/binary>>/binary,
                            "|transport="/utf8>>/binary,
                        (erlang:element(4, Runtime))/binary>>/binary,
                    "|session_store="/utf8>>/binary,
                (erlang:element(5, Runtime))/binary>>/binary,
            "|boot="/utf8>>/binary,
        (erlang:element(6, Runtime))/binary>>.

-file("src/lightspeed/platform/convention_profile.gleam", 227).
?DOC(" Stable profile signature.\n").
-spec signature(core_profile()) -> binary().
signature(Profile) ->
    <<<<<<<<<<<<<<<<<<<<<<"profile:"/utf8, (erlang:element(2, Profile))/binary>>/binary,
                                            "|runtime="/utf8>>/binary,
                                        (runtime_signature(
                                            erlang:element(3, Profile)
                                        ))/binary>>/binary,
                                    "|data="/utf8>>/binary,
                                (data_signature(erlang:element(4, Profile)))/binary>>/binary,
                            "|template="/utf8>>/binary,
                        (template_signature(erlang:element(5, Profile)))/binary>>/binary,
                    "|hidden_runtime_behavior="/utf8>>/binary,
                (bool_label(erlang:element(7, Profile)))/binary>>/binary,
            "|overrides="/utf8>>/binary,
        (override_seam_signature(Profile))/binary>>.

-file("src/lightspeed/platform/convention_profile.gleam", 243).
?DOC(" Reference fixture profiles for deterministic M45 certification.\n").
-spec reference_profiles() -> list(core_profile()).
reference_profiles() ->
    Baseline = default_profile(),
    Runtime_override = with_escape_hatch(Baseline, websocket_fallback_hatch()),
    Multi_override = begin
        _pipe = Baseline,
        _pipe@1 = with_escape_hatch(_pipe, websocket_fallback_hatch()),
        _pipe@2 = with_escape_hatch(_pipe@1, data_partition_hatch()),
        with_escape_hatch(_pipe@2, template_diagnostics_hatch())
    end,
    [Baseline, Runtime_override, Multi_override].

-file("src/lightspeed/platform/convention_profile.gleam", 256).
?DOC(" Deterministic fixture signatures for CI drift gates.\n").
-spec fixture_snapshots() -> list({binary(), binary()}).
fixture_snapshots() ->
    Profiles = reference_profiles(),
    case Profiles of
        [Baseline, Runtime_override, Multi_override] ->
            [{<<"default_profile"/utf8>>, signature(Baseline)},
                {<<"runtime_override_profile"/utf8>>,
                    signature(Runtime_override)},
                {<<"multi_override_profile"/utf8>>, signature(Multi_override)}];

        _ ->
            []
    end.

-file("src/lightspeed/platform/convention_profile.gleam", 270).
?DOC(" Deterministic M45 fixture snapshot signature.\n").
-spec snapshot_signature() -> binary().
snapshot_signature() ->
    Entries = begin
        _pipe = fixture_snapshots(),
        gleam@list:map(
            _pipe,
            fun(Entry) ->
                {Label, Value} = Entry,
                <<<<Label/binary, "="/utf8>>/binary, Value/binary>>
            end
        )
    end,
    <<<<<<"m45.profile.v"/utf8, (erlang:integer_to_binary(1))/binary>>/binary,
            "|"/utf8>>/binary,
        (join_with(<<";"/utf8>>, Entries))/binary>>.