syntax = "proto3";

package {{ spec.package }};

option java_multiple_files = true;
option java_outer_classname = "{{ spec.java_outer_classname }}";
option java_package = "{{ spec.package }}";
{% for import in spec.imports %}
import "{{ import }}";{% endfor %}

// TODO: describe this service.
service {{ spec.service_name }} {
{%- for method_group in spec.method_groups %}
  // Methods for {{ method_group.type }}.
  {% for method in method_group.methods -%}
  // {{ method.description }}
  rpc {{ method.name }}({{ method.request_type }}) returns ({{ method.response_type }}) {
    {%- for option in method.options %}
    option ({{ option.type}}) = {{ option.value }};
    {%- endfor %} 
  }
  {% endfor -%}
{% endfor %}
}

//////////////////////////////////////////////////////////
//                                                      //
//                  Resource Types                      //
//                                                      //
//////////////////////////////////////////////////////////
{% for resource in spec.resources -%}
// TODO: Describe the {{ resource.type }} type.
message {{ resource.type }} {
  option (google.api.resource) = {
    type: "{{ resource.domain }}/{{ resource.type }}"
    pattern: "{{ resource.pattern }}"
    singular: "{{ resource.singular }}"
    plural: "{{ resource.plural }}"
  };

  // The name of the {{ resource.type }}.
  string name = 1 [(google.api.field_behavior) = IDENTIFIER];
}

{% endfor -%}

//////////////////////////////////////////////////////////
//                                                      //
//               Requests & Responses                   //
//                                                      //
//////////////////////////////////////////////////////////
{%- macro options_list(options) -%}
{%- if not options -%}
{% else %} [{% for option in options %}
    ({{ option.type }}) = {{ option.value }}{{ "," if not loop.last }}{% endfor %}
  ]
{%- endif %}
{%- endmacro -%}

{%- for req_res in spec.req_res %}
// {{ req_res.description }}
message {{ req_res.type }} {
  {%- for field in req_res.fields %}
  {%- for line in field.comment_lines %}
  // {{ line }}
  {%- endfor %}
  {{ field.type }} {{ field.name }} = {{ loop.index }}{{ options_list(field.options) }}; 
  {%- endfor %}
}
{% endfor -%}