{% extends "prefixes.sparql" %}
{% block query %}
SELECT DISTINCT ?first_name      ?full_name       ?group_id
                ?uuid ?id        ?institution_id  ?is_active
                ?is_public       ?job_title       ?last_name
                ?orcid_id        ?url_name        ?order_index
                ?is_editable     ?email
WHERE {
  GRAPH <{{state_graph}}> {
    ?author            rdf:type                 shst:Author .
    OPTIONAL { ?author shst:id                  ?id . }

    {%- if item_uri is not none %}
    ?item_uri          rdf:type                 shst:{{prefix}} ;
                       shst:authors             ?authors .
    ?authors           rdf:rest*                ?rest .
    ?rest              rdf:first                ?author ;
                       shst:index               ?order_index .
    {%- endif %}
    {%- if account_uuid is not none %}
    ?container         rdf:type                 shst:{{prefix}}Container .
    {%- if is_published %}
    ?container         shst:published_versions/rdf:rest*/rdf:first ?item_uri .
    {%- else %}
    ?container         shst:draft               ?item_uri .
    {%- endif %}
    ?container         shst:account             ?account .

    {%- if not disable_collaboration %}
    OPTIONAL {
      ?item_uri          shst:container      ?container .
      ?collaborator      rdf:type            shst:Collaborator .
      ?collaborator      shst:item           ?item_uri .
      ?collaborator      shst:account        ?collaborator_account .
      ?collaborator      shst:metadata_read  "true"^^xsd:boolean .
    }
    OPTIONAL {
      ?group_uri          rdf:type shst:InstitutionGroup .
      ?item_uri           shst:association_criteria        ?association_criteria .
      ?group_uri          shst:association_criteria        ?association_criteria .
      ?group_uri          shst:members/rdf:rest*/rdf:first ?member_uri .
      ?member_uri         shst:is_supervisor "true"^^xsd:boolean .
      ?member_uri         shst:account ?member_account .
    }
    {%- endif %}
    {%- endif %}
    OPTIONAL { ?author shst:first_name          ?first_name . }
    OPTIONAL { ?author shst:full_name           ?full_name . }
    OPTIONAL { ?author shst:group_id            ?group_id . }
    OPTIONAL { ?author shst:institution_id      ?institution_id . }
    OPTIONAL { ?author shst:is_active           ?is_active . }
    OPTIONAL { ?author shst:is_public           ?is_public . }
    OPTIONAL { ?author shst:job_title           ?job_title . }
    OPTIONAL { ?author shst:last_name           ?last_name . }
    OPTIONAL { ?author shst:orcid_id            ?orcid_id . }
    OPTIONAL { ?author shst:url_name            ?url_name . }
    OPTIONAL { ?author shst:email               ?email . }

    {%- if account_uuid is not none %}
    BIND (xsd:boolean (EXISTS { ?author shst:created_by <account:{{account_uuid}}> . }) AS ?is_editable)
    {%- else %}
    BIND("false"^^xsd:boolean AS ?is_editable)
    {%- endif %}

    BIND(STRAFTER(STR(?author), "author:") AS ?uuid)
  }
{%- if account_uuid is not none %}
  {%- if disable_collaboration %}
FILTER (?account = <account:{{account_uuid}}>)
  {%- else %}
FILTER (?account = <account:{{account_uuid}}> ||
        ?collaborator_account = <account:{{account_uuid}}> ||
        ?member_account = <account:{{account_uuid}}>)
  {%- endif %}
{%- endif %}
{%- if author_uuid is not none %}
FILTER (?author = <author:{{author_uuid}}>)
{%- endif %}
{%- if item_uri is not none %}
  FILTER (?item_uri = <{{item_uri}}>)
{% endif %}
{% if filters is not none %}{{ filters | safe }}{% endif %}
}
{% endblock %}
