<?xml version="1.0" encoding="UTF-8"?>
<!--
  Begehung.pro data return, schema version 2.

  One archive per tenant: rueckgabe.xml (this schema, archived as rueckgabe.xsd) holds everything
  that is not in the inspection export (v<N>/data.xml) of a sealed version: company profile, team
  members, custom report templates with a reference to the Word file of their released revision,
  checklist templates in all versions, customers with contacts, projects, series and inspections,
  draft versions in full, sealed versions as a reference to their data.xml plus their PDFs with
  SHA-256, action reports, draft discussions of all versions, mailings not listed in a data.xml, and
  the missing parts of the archive.

  Changes against version 1: reportTemplate carries fileRef, the Word file of the revision released
  at the cutoff under vorlagen/; gap kind and gap refType REPORT_TEMPLATE for that file.

  The inspection export schema is included from schema/inspection-export-1.xsd. Its types Address,
  Areas, Checklists, Findings, Actions, Uuid, UtcDateTime, NonEmptyText, Token and Salutation and
  its attribute group EvidenceOwner are reused. Types of this schema carry the prefix Return,
  identity constraints (key, keyref, unique) the prefix return.

  Conventions
  - No target namespace. rueckgabe.xml points here via xsi:noNamespaceSchemaLocation.
  - Element and attribute names are English camelCase.
  - Every entity carries its database id as attribute "id" (UUID, lower case).
  - Cross references are attributes ending in "Ref" and hold the id of the referenced entity. A
    reference into a data.xml (actionRef, recipientRef, issueRef and checklistItemRef of a
    discussion) and resendOfRef are not checked by this schema; every other reference is.
  - Path attributes (fileRef, logoRef, avatarRef, photoRef, dataRef, folder) and gap/path are
    relative to the root folder of the archive. A path names the place of the file; when the file
    is missing, a gaps/gap with the same path says why.
  - Calendar values are xs:date; points in time are xs:dateTime in UTC with the suffix Z.
  - Enumerated values are the tokens of the Begehung.pro data model, verbatim.
  - An optional value that is empty in the data is omitted; list containers are always present.
  - Order is stable: customers and inspections by folder; versions and checklist template versions
    by number; items and sources by sortOrder, then id; posts by createdAt, then id; PDFs by
    fileRef; gaps by path, then kind; everything else by id. Inside a draft version the order of
    the inspection export applies.
  - UTF-8 without BOM, indented by two spaces, lines end with a line feed. Characters XML 1.0
    cannot carry are removed.
  - Every change to the schema ships as a new schema version with a new file name
    (data-return-<N>.xsd); a released version never changes.
-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

  <xs:include schemaLocation="schema/inspection-export-1.xsd"/>

  <xs:element name="dataReturn" type="ReturnDocument">
    <xs:key name="returnContactKey">
      <xs:selector xpath="customers/customer/contacts/contact"/>
      <xs:field xpath="@id"/>
    </xs:key>
    <xs:key name="returnProjectKey">
      <xs:selector xpath="customers/customer/projects/project"/>
      <xs:field xpath="@id"/>
    </xs:key>
    <xs:key name="returnVersionKey">
      <xs:selector xpath="customers/customer/series/series/inspections/inspection/versions/sealedVersion|customers/customer/series/series/inspections/inspection/versions/draftVersion"/>
      <xs:field xpath="@id"/>
    </xs:key>
    <xs:key name="returnReportTemplateKey">
      <xs:selector xpath="reportTemplates/reportTemplate"/>
      <xs:field xpath="@id"/>
    </xs:key>
    <xs:key name="returnChecklistVersionKey">
      <xs:selector xpath="checklistTemplates/checklistTemplate/versions/version"/>
      <xs:field xpath="@id"/>
    </xs:key>
    <xs:unique name="returnFileRefUnique">
      <xs:selector xpath="customers/customer/series/series/inspections/inspection/versions/draftVersion/evidence/photo|customers/customer/series/series/inspections/inspection/versions/draftVersion/evidence/audio|customers/customer/series/series/inspections/inspection/versions/draftVersion/signatures/signature|customers/customer/series/series/inspections/inspection/versions/sealedVersion/pdfs/pdf|reportTemplates/reportTemplate"/>
      <xs:field xpath="@fileRef"/>
    </xs:unique>
    <xs:keyref name="returnCompanyTemplateRef" refer="returnReportTemplateKey">
      <xs:selector xpath="company"/>
      <xs:field xpath="@reportTemplateRef"/>
    </xs:keyref>
    <xs:keyref name="returnCustomerTemplateRef" refer="returnReportTemplateKey">
      <xs:selector xpath="customers/customer"/>
      <xs:field xpath="@reportTemplateRef"/>
    </xs:keyref>
    <xs:keyref name="returnCurrentVersionRef" refer="returnChecklistVersionKey">
      <xs:selector xpath="checklistTemplates/checklistTemplate"/>
      <xs:field xpath="@currentVersionRef"/>
    </xs:keyref>
    <xs:keyref name="returnProjectContactRef" refer="returnContactKey">
      <xs:selector xpath="customers/customer/projects/project/contacts/contactRef"/>
      <xs:field xpath="@ref"/>
    </xs:keyref>
    <xs:keyref name="returnSeriesProjectRef" refer="returnProjectKey">
      <xs:selector xpath="customers/customer/series/series"/>
      <xs:field xpath="@projectRef"/>
    </xs:keyref>
    <xs:keyref name="returnAttendeeContactRef" refer="returnContactKey">
      <xs:selector xpath="customers/customer/series/series/inspections/inspection/versions/draftVersion/attendees/attendee"/>
      <xs:field xpath="@contactRef"/>
    </xs:keyref>
    <xs:keyref name="returnResponsibleContactRef" refer="returnContactKey">
      <xs:selector xpath="customers/customer/series/series/inspections/inspection/versions/draftVersion/actions/action/responsible"/>
      <xs:field xpath="@contactRef"/>
    </xs:keyref>
    <xs:keyref name="returnSignatureContactRef" refer="returnContactKey">
      <xs:selector xpath="customers/customer/series/series/inspections/inspection/versions/draftVersion/signatures/signature"/>
      <xs:field xpath="@contactRef"/>
    </xs:keyref>
    <xs:keyref name="returnActionReportVersionRef" refer="returnVersionKey">
      <xs:selector xpath="actionReports/actionReport"/>
      <xs:field xpath="@versionRef"/>
    </xs:keyref>
    <xs:keyref name="returnActionReportContactRef" refer="returnContactKey">
      <xs:selector xpath="actionReports/actionReport"/>
      <xs:field xpath="@reportedByContactRef"/>
    </xs:keyref>
    <xs:keyref name="returnDiscussionVersionRef" refer="returnVersionKey">
      <xs:selector xpath="discussions/discussion"/>
      <xs:field xpath="@versionRef"/>
    </xs:keyref>
    <xs:keyref name="returnPostContactRef" refer="returnContactKey">
      <xs:selector xpath="discussions/discussion/posts/post"/>
      <xs:field xpath="@contactRef"/>
    </xs:keyref>
    <xs:keyref name="returnMailingVersionRef" refer="returnVersionKey">
      <xs:selector xpath="mailings/mailing"/>
      <xs:field xpath="@versionRef"/>
    </xs:keyref>
    <xs:keyref name="returnMailingContactRef" refer="returnContactKey">
      <xs:selector xpath="mailings/mailing"/>
      <xs:field xpath="@contactRef"/>
    </xs:keyref>
  </xs:element>

  <xs:complexType name="ReturnDocument">
    <xs:sequence>
      <xs:element name="company" type="ReturnCompany"/>
      <xs:element name="teamMembers" type="ReturnTeamMembers"/>
      <xs:element name="reportTemplates" type="ReturnReportTemplates"/>
      <xs:element name="checklistTemplates" type="ReturnChecklistTemplates"/>
      <xs:element name="customers" type="ReturnCustomers"/>
      <xs:element name="actionReports" type="ReturnActionReports"/>
      <xs:element name="discussions" type="ReturnDiscussions"/>
      <xs:element name="mailings" type="ReturnMailings"/>
      <xs:element name="gaps" type="ReturnGaps"/>
    </xs:sequence>
    <xs:attribute name="schemaVersion" type="xs:positiveInteger" use="required" fixed="2"/>
    <xs:attribute name="cutoffAt" type="UtcDateTime" use="required">
      <xs:annotation>
        <xs:documentation>Stichtag: the server state at the moment the data return was requested. The whole archive shows this state.</xs:documentation>
      </xs:annotation>
    </xs:attribute>
  </xs:complexType>

  <!-- Simple types -->

  <xs:simpleType name="ReturnPath">
    <xs:annotation>
      <xs:documentation>Path relative to the root folder of the archive. A trailing slash names a folder.</xs:documentation>
    </xs:annotation>
    <xs:restriction base="xs:string">
      <xs:pattern value="[^/]+(/[^/]+)*/?"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="ReturnTemplatePath">
    <xs:annotation>
      <xs:documentation>Word file of a report template: vorlagen/, the template name without umlauts (at most 60 characters), an underscore and the first six characters of the template id.</xs:documentation>
    </xs:annotation>
    <xs:restriction base="xs:string">
      <xs:pattern value="vorlagen/[A-Za-z0-9\-]{1,60}_[0-9a-f\-]{6}\.docx"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="ReturnSha256">
    <xs:restriction base="xs:string">
      <xs:pattern value="[0-9a-f]{64}"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="ReturnRole">
    <xs:restriction base="xs:token">
      <xs:enumeration value="owner"/>
      <xs:enumeration value="admin"/>
      <xs:enumeration value="member"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="ReturnAuthorKind">
    <xs:restriction base="xs:token">
      <xs:enumeration value="CONTACT"/>
      <xs:enumeration value="TENANT"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="ReturnGapKind">
    <xs:restriction base="xs:token">
      <xs:enumeration value="PHOTO"/>
      <xs:enumeration value="AUDIO"/>
      <xs:enumeration value="SIGNATURE"/>
      <xs:enumeration value="CUSTOMER_LOGO"/>
      <xs:enumeration value="COMPANY_LOGO"/>
      <xs:enumeration value="AVATAR"/>
      <xs:enumeration value="EVIDENCE_PHOTO"/>
      <xs:enumeration value="PDF"/>
      <xs:enumeration value="REPORT_TEMPLATE"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="ReturnGapReason">
    <xs:restriction base="xs:token">
      <xs:enumeration value="NOT_UPLOADED"/>
      <xs:enumeration value="UNREADABLE"/>
      <xs:enumeration value="NO_PDF"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="ReturnGapRefType">
    <xs:restriction base="xs:token">
      <xs:enumeration value="VERSION"/>
      <xs:enumeration value="CUSTOMER"/>
      <xs:enumeration value="COMPANY"/>
      <xs:enumeration value="TEAM_MEMBER"/>
      <xs:enumeration value="REPORT_TEMPLATE"/>
    </xs:restriction>
  </xs:simpleType>

  <!-- Company -->

  <xs:complexType name="ReturnCompany">
    <xs:annotation>
      <xs:documentation>Company profile (tenant_profile). Always present; empty when the tenant has no profile. reportTemplateRef is set only for a custom report template.</xs:documentation>
    </xs:annotation>
    <xs:sequence>
      <xs:element name="name" type="NonEmptyText" minOccurs="0"/>
      <xs:element name="address" type="Address" minOccurs="0"/>
      <xs:element name="phone" type="NonEmptyText" minOccurs="0"/>
      <xs:element name="email" type="NonEmptyText" minOccurs="0"/>
      <xs:element name="website" type="NonEmptyText" minOccurs="0"/>
      <xs:element name="sender" type="ReturnSender" minOccurs="0"/>
    </xs:sequence>
    <xs:attribute name="logoRef" type="ReturnPath"/>
    <xs:attribute name="reportTemplateRef" type="Uuid"/>
  </xs:complexType>

  <xs:complexType name="ReturnSender">
    <xs:annotation>
      <xs:documentation>Sender of report mails. domainState is the verification state of the sender domain (open token list).</xs:documentation>
    </xs:annotation>
    <xs:sequence>
      <xs:element name="displayName" type="NonEmptyText" minOccurs="0"/>
      <xs:element name="replyTo" type="NonEmptyText" minOccurs="0"/>
      <xs:element name="address" type="NonEmptyText" minOccurs="0"/>
      <xs:element name="domainState" type="Token" minOccurs="0"/>
      <xs:element name="verifiedAt" type="UtcDateTime" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>

  <!-- Team members -->

  <xs:complexType name="ReturnTeamMembers">
    <xs:sequence>
      <xs:element name="member" minOccurs="0" maxOccurs="unbounded">
        <xs:annotation>
          <xs:documentation>Current member of the tenant with the role in this tenant. id is the user id.</xs:documentation>
        </xs:annotation>
        <xs:complexType>
          <xs:sequence>
            <xs:element name="name" type="NonEmptyText" minOccurs="0"/>
            <xs:element name="email" type="NonEmptyText" minOccurs="0"/>
          </xs:sequence>
          <xs:attribute name="id" type="Uuid" use="required"/>
          <xs:attribute name="role" type="ReturnRole" use="required"/>
          <xs:attribute name="avatarRef" type="ReturnPath"/>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>

  <!-- Report templates -->

  <xs:complexType name="ReturnReportTemplates">
    <xs:sequence>
      <xs:element name="reportTemplate" minOccurs="0" maxOccurs="unbounded">
        <xs:annotation>
          <xs:documentation>Custom report template of the tenant: name, and fileRef when the template had a released revision at the cutoff. fileRef names the Word file (vorlage.docx) of exactly that revision under vorlagen/; earlier revisions and trial files are not in the archive.</xs:documentation>
        </xs:annotation>
        <xs:complexType>
          <xs:sequence>
            <xs:element name="name" type="NonEmptyText" minOccurs="0"/>
          </xs:sequence>
          <xs:attribute name="id" type="Uuid" use="required"/>
          <xs:attribute name="fileRef" type="ReturnTemplatePath"/>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>

  <!-- Checklist templates -->

  <xs:complexType name="ReturnChecklistTemplates">
    <xs:sequence>
      <xs:element name="checklistTemplate" minOccurs="0" maxOccurs="unbounded">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="versions">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="version" type="ReturnChecklistTemplateVersion" minOccurs="0" maxOccurs="unbounded"/>
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
          <xs:attribute name="id" type="Uuid" use="required"/>
          <xs:attribute name="currentVersionRef" type="Uuid"/>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="ReturnChecklistTemplateVersion">
    <xs:sequence>
      <xs:element name="name" type="NonEmptyText"/>
      <xs:element name="description" type="NonEmptyText" minOccurs="0"/>
      <xs:element name="lockedAt" type="UtcDateTime" minOccurs="0"/>
      <xs:element name="supersededAt" type="UtcDateTime" minOccurs="0"/>
      <xs:element name="items">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="item" type="ReturnChecklistTemplateItem" minOccurs="0" maxOccurs="unbounded"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
    <xs:attribute name="id" type="Uuid" use="required"/>
    <xs:attribute name="number" type="xs:positiveInteger" use="required"/>
  </xs:complexType>

  <xs:complexType name="ReturnChecklistTemplateItem">
    <xs:sequence>
      <xs:element name="label" type="NonEmptyText"/>
      <xs:element name="hint" type="NonEmptyText" minOccurs="0"/>
      <xs:element name="sources">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="source" minOccurs="0" maxOccurs="unbounded">
              <xs:complexType>
                <xs:simpleContent>
                  <xs:extension base="NonEmptyText">
                    <xs:attribute name="id" type="Uuid" use="required"/>
                    <xs:attribute name="sortOrder" type="xs:long" use="required"/>
                  </xs:extension>
                </xs:simpleContent>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
    <xs:attribute name="id" type="Uuid" use="required"/>
    <xs:attribute name="sortOrder" type="xs:long" use="required"/>
  </xs:complexType>

  <!-- Customers -->

  <xs:complexType name="ReturnCustomers">
    <xs:sequence>
      <xs:element name="customer" type="ReturnCustomer" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="ReturnCustomer">
    <xs:annotation>
      <xs:documentation>folder is the customer folder of the archive. reportTemplateRef is set only for a custom report template.</xs:documentation>
    </xs:annotation>
    <xs:sequence>
      <xs:element name="name" type="NonEmptyText"/>
      <xs:element name="note" type="NonEmptyText" minOccurs="0"/>
      <xs:element name="website" type="NonEmptyText" minOccurs="0"/>
      <xs:element name="address" type="Address" minOccurs="0"/>
      <xs:element name="contacts">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="contact" type="ReturnContact" minOccurs="0" maxOccurs="unbounded"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="projects">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="project" type="ReturnProject" minOccurs="0" maxOccurs="unbounded"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="series">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="series" type="ReturnSeries" minOccurs="0" maxOccurs="unbounded"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
    <xs:attribute name="id" type="Uuid" use="required"/>
    <xs:attribute name="folder" type="ReturnPath" use="required"/>
    <xs:attribute name="logoRef" type="ReturnPath"/>
    <xs:attribute name="reportTemplateRef" type="Uuid"/>
  </xs:complexType>

  <xs:complexType name="ReturnContact">
    <xs:sequence>
      <xs:element name="salutation" type="Salutation" minOccurs="0"/>
      <xs:element name="firstName" type="NonEmptyText" minOccurs="0"/>
      <xs:element name="lastName" type="NonEmptyText"/>
      <xs:element name="role" type="NonEmptyText" minOccurs="0"/>
      <xs:element name="company" type="NonEmptyText" minOccurs="0"/>
      <xs:element name="email" type="NonEmptyText" minOccurs="0"/>
      <xs:element name="phone" type="NonEmptyText" minOccurs="0"/>
      <xs:element name="address" type="Address" minOccurs="0"/>
      <xs:element name="note" type="NonEmptyText" minOccurs="0"/>
    </xs:sequence>
    <xs:attribute name="id" type="Uuid" use="required"/>
  </xs:complexType>

  <xs:complexType name="ReturnProject">
    <xs:sequence>
      <xs:element name="name" type="NonEmptyText"/>
      <xs:element name="description" type="NonEmptyText" minOccurs="0"/>
      <xs:element name="address" type="Address" minOccurs="0"/>
      <xs:element name="contacts">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="contactRef" minOccurs="0" maxOccurs="unbounded">
              <xs:complexType>
                <xs:attribute name="ref" type="Uuid" use="required"/>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
    <xs:attribute name="id" type="Uuid" use="required"/>
  </xs:complexType>

  <xs:complexType name="ReturnSeries">
    <xs:sequence>
      <xs:element name="name" type="NonEmptyText"/>
      <xs:element name="description" type="NonEmptyText" minOccurs="0"/>
      <xs:element name="intervalDays" type="xs:positiveInteger" minOccurs="0"/>
      <xs:element name="startDate" type="xs:date" minOccurs="0"/>
      <xs:element name="inspections">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="inspection" type="ReturnInspection" minOccurs="0" maxOccurs="unbounded"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
    <xs:attribute name="id" type="Uuid" use="required"/>
    <xs:attribute name="projectRef" type="Uuid"/>
  </xs:complexType>

  <xs:complexType name="ReturnInspection">
    <xs:annotation>
      <xs:documentation>folder is the inspection folder of the archive. executionDate is the Durchfuehrungsdatum (inspection.inspection_date), not a sealing time. status known at version 1: DRAFT, SEALED.</xs:documentation>
    </xs:annotation>
    <xs:sequence>
      <xs:element name="name" type="NonEmptyText" minOccurs="0"/>
      <xs:element name="description" type="NonEmptyText" minOccurs="0"/>
      <xs:element name="executionDate" type="xs:date" minOccurs="0"/>
      <xs:element name="status" type="Token"/>
      <xs:element name="versions">
        <xs:complexType>
          <xs:choice minOccurs="0" maxOccurs="unbounded">
            <xs:element name="sealedVersion" type="ReturnSealedVersion"/>
            <xs:element name="draftVersion" type="ReturnDraftVersion">
              <xs:key name="returnDraftAreaKey">
                <xs:selector xpath="areas/area"/>
                <xs:field xpath="@id"/>
              </xs:key>
              <xs:key name="returnDraftChecklistItemKey">
                <xs:selector xpath="checklists/checklist/items/item"/>
                <xs:field xpath="@id"/>
              </xs:key>
              <xs:key name="returnDraftFindingKey">
                <xs:selector xpath="findings/finding"/>
                <xs:field xpath="@id"/>
              </xs:key>
              <xs:keyref name="returnDraftFindingAreaRef" refer="returnDraftAreaKey">
                <xs:selector xpath="findings/finding"/>
                <xs:field xpath="@areaRef"/>
              </xs:keyref>
              <xs:keyref name="returnDraftFindingChecklistItemRef" refer="returnDraftChecklistItemKey">
                <xs:selector xpath="findings/finding"/>
                <xs:field xpath="@checklistItemRef"/>
              </xs:keyref>
              <xs:keyref name="returnDraftEvidenceFindingRef" refer="returnDraftFindingKey">
                <xs:selector xpath="evidence/photo|evidence/audio|evidence/note"/>
                <xs:field xpath="@findingRef"/>
              </xs:keyref>
              <xs:keyref name="returnDraftEvidenceAreaRef" refer="returnDraftAreaKey">
                <xs:selector xpath="evidence/photo|evidence/audio|evidence/note"/>
                <xs:field xpath="@areaRef"/>
              </xs:keyref>
              <xs:keyref name="returnDraftActionFindingRef" refer="returnDraftFindingKey">
                <xs:selector xpath="actions/action"/>
                <xs:field xpath="@findingRef"/>
              </xs:keyref>
            </xs:element>
          </xs:choice>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
    <xs:attribute name="id" type="Uuid" use="required"/>
    <xs:attribute name="folder" type="ReturnPath" use="required"/>
  </xs:complexType>

  <!-- Versions -->

  <xs:complexType name="ReturnSealedVersion">
    <xs:annotation>
      <xs:documentation>Sealed version. Its content is the inspection export at dataRef (generated at the cutoff). pdfs lists every stored PDF of the version that is in the archive; sha256 is the digest of the file.</xs:documentation>
    </xs:annotation>
    <xs:sequence>
      <xs:element name="sealedAt" type="UtcDateTime"/>
      <xs:element name="pdfs">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="pdf" minOccurs="0" maxOccurs="unbounded">
              <xs:complexType>
                <xs:attribute name="fileRef" type="ReturnPath" use="required"/>
                <xs:attribute name="sha256" type="ReturnSha256" use="required"/>
                <xs:attribute name="templateIdentity" type="NonEmptyText"/>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
    <xs:attribute name="id" type="Uuid" use="required"/>
    <xs:attribute name="number" type="xs:positiveInteger" use="required"/>
    <xs:attribute name="dataRef" type="ReturnPath" use="required"/>
  </xs:complexType>

  <xs:complexType name="ReturnDraftVersion">
    <xs:annotation>
      <xs:documentation>Draft version in full, built like the inspection export. File references point into the draft folder of the inspection.</xs:documentation>
    </xs:annotation>
    <xs:sequence>
      <xs:element name="startedAt" type="UtcDateTime"/>
      <xs:element name="comment" type="NonEmptyText" minOccurs="0"/>
      <xs:element name="attendees" type="ReturnAttendees"/>
      <xs:element name="areas" type="Areas"/>
      <xs:element name="checklists" type="Checklists"/>
      <xs:element name="findings" type="Findings"/>
      <xs:element name="evidence" type="ReturnEvidence"/>
      <xs:element name="actions" type="Actions"/>
      <xs:element name="signatures" type="ReturnSignatures"/>
    </xs:sequence>
    <xs:attribute name="id" type="Uuid" use="required"/>
    <xs:attribute name="number" type="xs:positiveInteger" use="required"/>
  </xs:complexType>

  <xs:complexType name="ReturnAttendees">
    <xs:sequence>
      <xs:element name="attendee" minOccurs="0" maxOccurs="unbounded">
        <xs:annotation>
          <xs:documentation>Exactly one of contactRef and inspector="true" is set.</xs:documentation>
        </xs:annotation>
        <xs:complexType>
          <xs:attribute name="id" type="Uuid" use="required"/>
          <xs:attribute name="contactRef" type="Uuid"/>
          <xs:attribute name="inspector" type="xs:boolean" default="false"/>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="ReturnEvidence">
    <xs:annotation>
      <xs:documentation>Photos, audio recordings and notes of the draft. At most one of findingRef and areaRef is set; neither = attached to the version itself.</xs:documentation>
    </xs:annotation>
    <xs:choice minOccurs="0" maxOccurs="unbounded">
      <xs:element name="photo">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="createdAt" type="UtcDateTime"/>
          </xs:sequence>
          <xs:attributeGroup ref="EvidenceOwner"/>
          <xs:attribute name="fileRef" type="ReturnPath" use="required"/>
        </xs:complexType>
      </xs:element>
      <xs:element name="audio">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="createdAt" type="UtcDateTime"/>
            <xs:element name="durationSeconds" type="xs:nonNegativeInteger" minOccurs="0"/>
            <xs:element name="transcript" minOccurs="0">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="text" type="NonEmptyText" minOccurs="0"/>
                </xs:sequence>
                <xs:attribute name="state" type="Token" use="required"/>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
          <xs:attributeGroup ref="EvidenceOwner"/>
          <xs:attribute name="fileRef" type="ReturnPath" use="required"/>
        </xs:complexType>
      </xs:element>
      <xs:element name="note">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="createdAt" type="UtcDateTime"/>
            <xs:element name="text" type="NonEmptyText"/>
          </xs:sequence>
          <xs:attributeGroup ref="EvidenceOwner"/>
        </xs:complexType>
      </xs:element>
    </xs:choice>
  </xs:complexType>

  <xs:complexType name="ReturnSignatures">
    <xs:sequence>
      <xs:element name="signature" minOccurs="0" maxOccurs="unbounded">
        <xs:annotation>
          <xs:documentation>At most one of contactRef and inspector="true" is set.</xs:documentation>
        </xs:annotation>
        <xs:complexType>
          <xs:sequence>
            <xs:element name="signedAt" type="UtcDateTime"/>
            <xs:element name="origin" type="Token"/>
          </xs:sequence>
          <xs:attribute name="id" type="Uuid" use="required"/>
          <xs:attribute name="contactRef" type="Uuid"/>
          <xs:attribute name="inspector" type="xs:boolean" default="false"/>
          <xs:attribute name="fileRef" type="ReturnPath" use="required"/>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>

  <!-- Action reports -->

  <xs:complexType name="ReturnActionReports">
    <xs:sequence>
      <xs:element name="actionReport" minOccurs="0" maxOccurs="unbounded">
        <xs:annotation>
          <xs:documentation>Report of a responsible person on an action (action_report). actionRef names the action in the data.xml or draft of versionRef. status known at version 1: GEMELDET, BESTAETIGT, VERWORFEN.</xs:documentation>
        </xs:annotation>
        <xs:complexType>
          <xs:sequence>
            <xs:element name="status" type="Token"/>
            <xs:element name="reportedAt" type="UtcDateTime"/>
            <xs:element name="note" type="NonEmptyText" minOccurs="0"/>
            <xs:element name="reviewedAt" type="UtcDateTime" minOccurs="0"/>
            <xs:element name="reviewNote" type="NonEmptyText" minOccurs="0"/>
          </xs:sequence>
          <xs:attribute name="id" type="Uuid" use="required"/>
          <xs:attribute name="actionRef" type="Uuid" use="required"/>
          <xs:attribute name="versionRef" type="Uuid" use="required"/>
          <xs:attribute name="reportedByContactRef" type="Uuid"/>
          <xs:attribute name="photoRef" type="ReturnPath"/>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>

  <!-- Discussions -->

  <xs:complexType name="ReturnDiscussions">
    <xs:sequence>
      <xs:element name="discussion" minOccurs="0" maxOccurs="unbounded">
        <xs:annotation>
          <xs:documentation>Discussion of the draft review on any version (draft_discussion). anchorKind known at version 1: INSPECTION, CHECKLIST_ITEM, ISSUE; status: OPEN, RESOLVED.</xs:documentation>
        </xs:annotation>
        <xs:complexType>
          <xs:sequence>
            <xs:element name="anchorLabel" type="NonEmptyText" minOccurs="0"/>
            <xs:element name="formerAnchorKind" type="Token" minOccurs="0"/>
            <xs:element name="formerAnchorLabel" type="NonEmptyText" minOccurs="0"/>
            <xs:element name="status" type="Token"/>
            <xs:element name="openedByName" type="NonEmptyText"/>
            <xs:element name="createdAt" type="UtcDateTime"/>
            <xs:element name="statusChangedAt" type="UtcDateTime" minOccurs="0"/>
            <xs:element name="statusChangedByName" type="NonEmptyText" minOccurs="0"/>
            <xs:element name="posts">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="post" minOccurs="0" maxOccurs="unbounded">
                    <xs:annotation>
                      <xs:documentation>authorKind CONTACT = written by the contact contactRef, TENANT = written by a team member; absent when the author is gone.</xs:documentation>
                    </xs:annotation>
                    <xs:complexType>
                      <xs:sequence>
                        <xs:element name="authorName" type="NonEmptyText"/>
                        <xs:element name="createdAt" type="UtcDateTime"/>
                        <xs:element name="body" type="NonEmptyText"/>
                      </xs:sequence>
                      <xs:attribute name="id" type="Uuid" use="required"/>
                      <xs:attribute name="authorKind" type="ReturnAuthorKind"/>
                      <xs:attribute name="contactRef" type="Uuid"/>
                    </xs:complexType>
                  </xs:element>
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
          <xs:attribute name="id" type="Uuid" use="required"/>
          <xs:attribute name="versionRef" type="Uuid" use="required"/>
          <xs:attribute name="anchorKind" type="Token" use="required"/>
          <xs:attribute name="issueRef" type="Uuid"/>
          <xs:attribute name="checklistItemRef" type="Uuid"/>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>

  <!-- Mailings -->

  <xs:complexType name="ReturnMailings">
    <xs:sequence>
      <xs:element name="mailing" minOccurs="0" maxOccurs="unbounded">
        <xs:annotation>
          <xs:documentation>Mail of the tenant that no data.xml lists: action tasks, draft mails and superseded report mails. The current report mail of a recipient of a sealed version is in its data.xml. A superseded report mail reaches recipientRef and versionRef through its successors (resendOfRef). kind and status are open token lists.</xs:documentation>
        </xs:annotation>
        <xs:complexType>
          <xs:sequence>
            <xs:element name="kind" type="Token"/>
            <xs:element name="recipient" type="NonEmptyText"/>
            <xs:element name="subject" type="NonEmptyText" minOccurs="0"/>
            <xs:element name="status" type="Token"/>
            <xs:element name="createdAt" type="UtcDateTime"/>
            <xs:element name="sentAt" type="UtcDateTime" minOccurs="0"/>
          </xs:sequence>
          <xs:attribute name="id" type="Uuid" use="required"/>
          <xs:attribute name="versionRef" type="Uuid"/>
          <xs:attribute name="recipientRef" type="Uuid"/>
          <xs:attribute name="contactRef" type="Uuid"/>
          <xs:attribute name="resendOfRef" type="Uuid"/>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>

  <!-- Gaps -->

  <xs:complexType name="ReturnGaps">
    <xs:sequence>
      <xs:element name="gap" minOccurs="0" maxOccurs="unbounded">
        <xs:annotation>
          <xs:documentation>Missing part of the archive. path is where the file would be (NO_PDF: the pdf folder of the version). refType and refId name the owner: a version, a customer, the company, a team member or a report template; refLabel describes it for people.</xs:documentation>
        </xs:annotation>
        <xs:complexType>
          <xs:sequence>
            <xs:element name="path" type="ReturnPath"/>
            <xs:element name="refLabel" type="NonEmptyText" minOccurs="0"/>
          </xs:sequence>
          <xs:attribute name="kind" type="ReturnGapKind" use="required"/>
          <xs:attribute name="reason" type="ReturnGapReason" use="required"/>
          <xs:attribute name="refType" type="ReturnGapRefType" use="required"/>
          <xs:attribute name="refId" type="Uuid" use="required"/>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>

</xs:schema>
