{#- This is a template that can convert 
   an xRegistry document 
   into an OpenAPI 3.0 document -#}
openapi: 3.0.0
info:
  title: "{{ title }}"
  version: "1.0"
{%- if root.endpoints %} 
servers:
{%- for endpointid, endpoint in root.endpoints.items() -%}
{%- set protocol = endpoint.protocol | lower -%}
{%- if endpoint.usage and "producer" in endpoint.usage and protocol == "http" -%}
{%- set options = endpoint.protocoloptions -%}
{%- set endpoints = endpoint.endpoints -%}
{%- for endpointObject in endpoints %}
- url: {{ endpointObject.uri }}
{%- if endpoint.description %}
  description: {{ endpoint.description }}
{%- endif -%}
{%- endfor -%}  
{%- endif -%}
{%- endfor -%}
{%- endif %}
paths:
  /:
    post:
      operationId: "cloudEventsWebhook"
      requestBody:
        required: true
        content:
          application/json:
            schema:
              oneOf:
                {%- for messagegroupid, messagegroup in root.messagegroups.items() -%}
                    {%- for messageid, message in messagegroup.messages.items() %}
                  - $ref: "#/components/schemas/{{ messageid }}"
                    {%- endfor -%}
                {%- endfor %}
      responses:
        "202":
          description: "Accepted"
        "400":
          description: "Bad Request"
        "415":
          description: "Unsupported Media Type"
components:
  schemas:
  {%- for messagegroupid, messagegroup in root.messagegroups.items() -%}
    {%- for messageid, message in messagegroup.messages.items() %}
    {{ messageid }}:
      type: "object"
      properties:
        {%- for attributeKey, attribute in message.envelopemetadata.items() %}
        "{{ attributeKey }}":
          {%- if attribute.type == "datetime" or attribute.type == "timestamp" %}
          type: "string"
          format: "date-time"
          {%- elif attribute.type == "uri" %}
          type: "string"
          format: "uri"
          {%- elif attribute.type == "uri-reference" %}
          type: "string"
          format: "uri-reference"
          {%- elif attribute.type == "uri-template" %}
          type: "string"
          format: "uri-reference"
          {%- elif attribute.type == "binary" %}
          type: "string"
          format: "binary"
          {%- elif attribute.type == "boolean" %}
          type: "boolean"
          {%- elif attribute.type == "integer" %}
          type: "integer"
          {%- elif attribute.type == "number" %}
          type: "number"
          {%- else %}
          type: "string"
          {%- endif %}
          {%- if attribute.value %}
          default: "{{ attribute.value }}"
          {%- endif %}
        {%- endfor %}
        data:
        {%- if message.dataschemauri %}
            {%- if message.dataschemauri.startswith('#') %}
            $ref: "#/components/schemas/{{ message.dataschemauri[1:].split('/')[-3] | pascal }}.{{ message.dataschemauri[1:].split('/')[-1] | pascal }}"
            {%- else %}
            $ref: "{{ message.dataschemauri }}"
            {%- endif %}
        {%- else %}
            type: "object"
        {%- endif %}
      required:
        {%- for attributeKey, attribute in message.envelopemetadata.items() %}
        {%- if attribute.required %}
        - "{{ attributeKey }}"
        {%- endif %}
        {%- endfor %}
    {%- endfor -%}
  {%- endfor -%}
  {%- if root.schemagroups is defined -%}
    {%- for schemagroupid, schemagroup in root.schemagroups.items() -%}
       {%- for schemaid, schema in schemagroup.schemas.items() -%}
          {%- set ns = namespace(newest_schemaversion=null) -%}
          {%- for schemaversionid, schemaversion in schema.versions.items() -%}
             {%- if newest_schemaversion is not defined or schemaversionid > newest_schemaversion -%}
                {%- set ns.newest_schemaversion = schemaversion -%}
             {%- endif -%}
          {%- endfor -%}
          {%- if ns.newest_schemaversion is defined %} 
    {{ schemagroupid | pascal }}.{{ schemaid | pascal }}:
            {%- if ns.newest_schemaversion.schemaurl -%}
            {%- if ns.newest_schemaversion.schemaurl.startswith('#') %}
            $ref: "#/components/schemas/{{ ns.newest_schemaversion.schemaurl.split('#/schemagroups/')[1] }}"
            {%- else %}
            $ref: "{{ ns.newest_schemaversion.schemaurl }}"
            {%- endif -%}
            {%- else %} 
      {{ ns.newest_schemaversion.schema | toyaml(3) | indent(6) }}
            {%- endif %} 
            {%- endif -%}
       {%- endfor -%} 
  {%- endfor -%}
  {%- endif -%}