---
openapi: "3.0.0"

info:
  version: 1.0.0
  title: '{{ package.documentation }}'

servers:
- url: http://127.0.0.1:8000/{env}/api
  variables:
    env:
      enum:
        - dev
        - test
        - prod
      default: dev

paths:{% for cls in package.classes %}{% if cls.is_abstract == False and cls.id_attribute %}
  "{{ package.path | snakecase }}{{ cls.get_name() | snakecase }}/":
    get:
      operationId: {{ package.path[1:] | snakecase | replace('/', '_') }}{{ cls.get_name() | snakecase }}_list
      parameters:
        - in: query
          name: page_key
          schema:
            type: integer
        - in: query
          name: limit
          description: 'The max number of records to return'
          schema:
            type: integer{% for assoc in cls.associations_from %}{% if assoc.cardinality.name in ["MANY_TO_ONE", "ONE_TO_ONE"] %}
        - in: query
          name: {{ assoc.destination_name | snakecase }}
          schema:
            type: {{ assoc.destination.id_attribute.dest_type }}{% endif %}{% endfor %}
      description: |
        {{ cls.documentation.replace("\n","\n        ") }}
      responses:
        200:
          description: ''
          content:
            application/json:
              schema:
                "$ref": "#/components/schemas/{{ cls.get_name() | camelcase }}_list"

  "{{ package.path | snakecase }}{{ cls.get_name() | snakecase }}/{% raw %}{{% endraw %}{{ cls.id_attribute.name | snakecase }}{% raw %}}{% endraw %}/":
    get:
      operationId: {{ package.path[1:] | snakecase | replace('/', '_') }}{{ cls.get_name() | snakecase }}_entity
      parameters:
        - name: {{ cls.id_attribute.name | snakecase }}
          in: path
          required: true
          schema:
            type: {{ cls.id_attribute.dest_type }}
      description: |
        {{ cls.documentation.replace("\n","\n        ") }}
      responses:
        200:
          description: ''
          content:
            application/json:
              schema:
                "$ref": "#/components/schemas/{{ cls.get_name() | camelcase }}"{% if cls.associations_from != [] or cls.associations_to != [] %}
          links:{% for assoc in cls.associations_from %}{% if assoc.destination.is_abstract == False %}
            {{ assoc.destination_name | snakecase }}:
              operationId: {{ assoc.destination.package.path[1:] | snakecase | replace('/', '_') }}{{ assoc.destination.name | snakecase }}_entity
              parameters:
                {{ assoc.destination.id_attribute.name | snakecase }}: '$response.body#/{{ assoc.destination_name | snakecase }}'{% endif %}{% endfor %}{% for assoc in cls.associations_to %}{% if assoc.destination.is_abstract == False %}
            {{ assoc.source_name | snakecase }}:
              operationId: {{ assoc.source.package.path[1:] | snakecase | replace('/', '_') }}{{ assoc.source.name | snakecase }}_list
              parameters:
                {{ assoc.destination_name | snakecase }}: $request.path.{{ cls.id_attribute.name | snakecase }}{% endif %}{% endfor %}{% endif %}
{% endif %}{% endfor %}
components:
  schemas:{% for cls in package.classes %}
    {{ cls.get_name() | camelcase }}_list:
      type: object
      description: 'List of {{ cls.get_name() }}'
      properties:
        result:
          type: array
          items:
            $ref: "#/components/schemas/{{ cls.get_name() | camelcase }}"
        next_page_key:
          type: string

    {{ cls.get_name() | camelcase }}_basic:
      type: object
      description: 'Basic view of {{ cls.get_name() }}'
      properties:{% if cls.id_attribute %}
        object_url:
          type: string
          format: uri
          description: "The URL to this object"
          readOnly: true
        object_type:
          type: string
          description: "The name of this object type"
          readOnly: true{% endif %}{% for attr in cls.attributes %}
        {{ attr.name | snakecase }}:
          description: '{{ attr.documentation.replace("\n","|") }}'
          type: {% if attr.dest_type in ["date","date-time","datetime","date time"] %}string
          format: date-time{% elif attr.classification %}string
          enum:{% for enum in attr.classification.values %}
          - {{ enum }}{% endfor %}{% elif attr.dest_type == "float" %}number
          format: float{% else %}{{ attr.dest_type }}{% endif %}{% endfor %}

    {{ cls.get_name() | camelcase }}:
      {% if not cls.supertype %}type: object
      description: 'Basic view of {{ cls.get_name() }}'
      properties:
        object_url:
          type: string
          format: uri
          description: "The URL to this object"
          readOnly: true
        object_type:
          type: string
          description: "The name of this object type"
          readOnly: true{% for attr in cls.attributes %}
        {{ attr.name | snakecase }}:
          description: '{{ attr.documentation.replace("\n","|") }}'
          type: {% if attr.dest_type in ["date","date-time","datetime","date time"] %}string
          format: date-time{% elif attr.classification %}string
          enum:{% for enum in attr.classification.values %}
          - {{ enum }}{% endfor %}{% elif attr.dest_type == "float" %}number
          format: float{% else %}{{ attr.dest_type }}{% endif %}{% endfor %}{% for assoc in cls.associations_from %}
        {{ assoc.destination_name | snakecase }}:
          {% if assoc.cardinality.name not in [ "MANY_TO_MANY", "ONE_TO_MANY" ] %}{% if assoc.destination.name != cls.get_name() %}{% if assoc.destination.package.name == cls.package.name %}$ref: "#/components/schemas/{{ assoc.destination.name | camelcase }}_basic"{% else %}$ref: "./{{ assoc.destination.package.name | camelcase }}.yaml#/components/schemas/{{ assoc.destination.name | camelcase }}_basic"{% endif %}{% else %}  type: {{ assoc.destination.id_attribute.dest_type }}{% endif %}{% else %}type: array
          items:
            oneOf:
              - type: {{ assoc.destination.id_attribute.dest_type }}
                format: uri
              - {% if assoc.destination.package.name == cls.package.name %}$ref: "#/components/schemas/{{ assoc.destination.name | camelcase }}_basic"{% else %}$ref: "./{{ assoc.destination.package.name | camelcase }}.yaml#/components/schemas/{{ assoc.destination.name | camelcase }}_basic"{% endif %}{% endif %}{% endfor %}{% for assoc in cls.associations_to %}{% if assoc.association_type.name == "COMPOSITION" %}
        {{ assoc.source_name | snakecase }}:
          {% if assoc.cardinality.name == "MANY_TO_ONE" %}type: array
          items:
            {% endif %}$ref: "#/components/schemas/{{ assoc.source.name | camelcase }}_basic"{% endif %}{% endfor %}{% else %}allOf:
        - $ref: "#/components/schemas/{{ cls.supertype.name | camelcase }}"
        - type: object{% if cls.attributes|length !=0 %}
          properties:{% endif %}{% for attr in cls.attributes %}
            {{ attr.name | snakecase }}:
              type: {% if attr.dest_type in ["date","date-time","datetime","date time"] %}string
              format: date-time{% elif attr.classification %}string
              enum:{% for enum in attr.classification.values %}
              - {{ enum }}{% endfor %}{% elif attr.dest_type == "float" %}number
              format: float{% else %}{{ attr.dest_type }}{% endif %}{% endfor %}{% for assoc in cls.associations_from %}
            {{ assoc.destination_name | snakecase }}:
              {% if assoc.cardinality.name not in [ "MANY_TO_MANY", "ONE_TO_MANY" ] %}{% if assoc.destination.name != cls.get_name() %}{% if assoc.destination.package.name == cls.package.name %}$ref: "#/components/schemas/{{ assoc.destination.name | camelcase }}_basic"{% else %}$ref: "./{{ assoc.destination.package.name | camelcase }}.yaml#/components/schemas/{{ assoc.destination.name | camelcase }}_basic"{% endif %}{% else %}  type: {{ assoc.destination.id_attribute.dest_type }}{% endif %}{% else %}type: array
              items:
                oneOf:
                  - type: {{ assoc.destination.id_attribute.dest_type }}
                    format: uri
                  - {% if assoc.destination.package.name == cls.package.name %}$ref: "#/components/schemas/{{ assoc.destination.name | camelcase }}_basic"{% else %}$ref: "./{{ assoc.destination.package.name | camelcase }}.yaml#/components/schemas/{{ assoc.destination.name | camelcase }}_basic"{% endif %}{% endif %}{% endfor %}{% endif %}
{% endfor %}
