<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

  <xs:simpleType name="InformationClassEnum">
    <xs:restriction base="xs:NCName">
      <xs:enumeration value="User"/>
      <xs:enumeration value="Computer"/>
      <xs:enumeration value="Ticket"/>
      <xs:enumeration value="VirtualSession"/>
      <xs:enumeration value="MobileDevice"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="OriginEnum">
    <xs:restriction base="xs:NCName">
      <xs:enumeration value="Main"/>
      <xs:enumeration value="NexthinkNxql"/>
      <xs:enumeration value="ActiveDirectory"/>
      <xs:enumeration value="AzureAD"/>
      <xs:enumeration value="F4sdAgent"/>
      <xs:enumeration value="M42Wpm"/>
      <xs:enumeration value="Intune"/>
      <xs:enumeration value="Citrix"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="TableTypeEnum">
    <xs:restriction base="xs:NCName">
      <xs:enumeration value="Static"/>
      <xs:enumeration value="History"/>
      <xs:enumeration value="HistoryEvents"/>
      <xs:enumeration value="Events"/>
      <xs:enumeration value="Selection"/>
      <xs:enumeration value="StaticDetails"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="TableCachedEnum">
    <xs:restriction base="xs:NCName">
      <xs:enumeration value="Default"/>
      <xs:enumeration value="Yes"/>
      <xs:enumeration value="No"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="StaticSourceTypeEnum">
    <xs:restriction base="xs:NCName">
      <xs:enumeration value="Query"/>
      <xs:enumeration value="Static"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="TableColumnTypeEnum">
    <xs:restriction base="xs:NCName">
      <xs:enumeration value="string"/>
      <xs:enumeration value="guid"/>
      <xs:enumeration value="int"/>
      <xs:enumeration value="bigint"/>
      <xs:enumeration value="float"/>
      <xs:enumeration value="datetime"/>
      <xs:enumeration value="version"/>
      <xs:enumeration value="md5"/>
      <xs:enumeration value="sid"/>
      <xs:enumeration value="ipv4"/>
      <xs:enumeration value="ipv6"/>
      <xs:enumeration value="text"/>
      <xs:enumeration value="boolean"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="TableColumnCharCaseEnum">
    <xs:restriction base="xs:NCName">
      <xs:enumeration value="none"/>
      <xs:enumeration value="lower"/>
      <xs:enumeration value="upper"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="AggregationEnum">
    <xs:restriction base="xs:NCName">
      <xs:enumeration value="first"/>
      <xs:enumeration value="min"/>
      <xs:enumeration value="max"/>
      <xs:enumeration value="sum"/>
      <xs:enumeration value="average"/>
      <xs:enumeration value="count"/>
      <xs:enumeration value="valuecount"/>
      <xs:enumeration value="latestValue"/>
      <xs:enumeration value="latestTime"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="M42PickupSortEnum">
    <xs:restriction base="xs:NCName">
      <xs:enumeration value="byPosition"/>
      <xs:enumeration value="byName"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="guid">
    <xs:restriction base="xs:string">
      <xs:pattern value="[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="ColumnList">
    <xs:list itemType="xs:NCName"/>
  </xs:simpleType>

  <xs:element name="F4SD-DataClusters-Configuration">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="InformationClasses" minOccurs="1" maxOccurs="1"/>
        <xs:element ref="DataCluster" minOccurs="1" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>

    <xs:key name="TableId">
      <xs:selector xpath="./DataCluster/Table"/>
      <xs:field xpath="@Name"/>
    </xs:key>

    <xs:unique name="UniqueTables">
      <xs:selector xpath="./DataCluster/Table"/>
      <xs:field xpath="@Name"/>
    </xs:unique>

  </xs:element>

  <xs:element name="InformationClasses">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="InformationClass" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:element name="InformationClass">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="InformationClass-Column" minOccurs="1" maxOccurs="unbounded"/>
      </xs:sequence>
      <xs:attribute name="Name" type="xs:NCName" use="required"/>
      <xs:attribute name="Description" type="xs:string" use="optional"/>
    </xs:complexType>
  </xs:element>

  <xs:complexType name="TableColumnBaseType">
    <xs:choice>
      <xs:element ref="DataCategories" minOccurs="0" maxOccurs="1" />
    </xs:choice>
    <xs:attribute name="Name" type="xs:NCName" use="required"/>
    <xs:attribute name="Type" use="required" type="TableColumnTypeEnum"/>
    <xs:attribute name="Cardinal" type="xs:int" use="optional"/>
    <xs:attribute name="Default" type="xs:string" use="optional"/>
    <xs:attribute name="CharCase" type="TableColumnCharCaseEnum" use="optional"/>
    <xs:attribute name="Description" type="xs:string" use="optional"/>
    <xs:attribute name="DisplayName" type="xs:string" use="optional"/>
  </xs:complexType>

  <xs:element  name="DataCategories">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Category" minOccurs="1" maxOccurs="unbounded">
          <xs:simpleType>
            <xs:restriction base="xs:string" />
          </xs:simpleType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:element name="InformationClass-Column">
    <xs:complexType>
      <xs:complexContent>
        <xs:extension base="TableColumnBaseType">
          <xs:attribute name="Index" type="xs:boolean" use="optional"/>
          <xs:attribute name="Search" type="xs:boolean" use="optional"/>
          <xs:attribute name="Reverse" type="xs:boolean" use="optional"/>
        </xs:extension>
      </xs:complexContent>
    </xs:complexType>
  </xs:element>

  <xs:element name="DataCluster">
    <xs:complexType>
      <xs:sequence>
        <xs:choice maxOccurs="unbounded" minOccurs="1">
          <xs:element ref="Table"/>
          <xs:element ref="JoinedTable"/>
        </xs:choice>
      </xs:sequence>
      <xs:attribute name="Name" type="xs:NCName" use="required"/>
      <xs:attribute name="Origin" use="required" type="OriginEnum"/>
      <xs:attribute name="InformationClass" type="InformationClassEnum" use="required"/>
      <xs:attribute name="Description" type="xs:string" use="optional"/>
    </xs:complexType>
  </xs:element>

  <xs:complexType name="TableBaseType">
    <xs:sequence>
      <xs:choice>
        <xs:element ref="Nexthink-NXQL-Template" maxOccurs="1" minOccurs="0"/>
        <xs:element ref="Matrix42-Pickup-Template" maxOccurs="1" minOccurs="0"/>
        <xs:element ref="Matrix42-DataQueryItems-Template" maxOccurs="1" minOccurs="0"/>
      </xs:choice>
      <xs:element ref="Table-Columns" maxOccurs="1" minOccurs="1"/>
    </xs:sequence>
    <xs:attribute name="Name" type="xs:NCName" use="required"/>
    <xs:attribute name="Description" type="xs:string" use="optional"/>
  </xs:complexType>

  <xs:element name="Table">
    <xs:complexType>
      <xs:complexContent>
        <xs:extension base="TableBaseType">
          <xs:sequence>
            <xs:element ref="AdditionalTableName" maxOccurs="unbounded" minOccurs="0"/>
            <xs:element ref="AutoCreate-Historic-Table" maxOccurs="unbounded" minOccurs="0"/>
            <xs:element ref="Table-Reference" maxOccurs="unbounded" minOccurs="0"/>
            <xs:element ref="Table-Index" maxOccurs="unbounded" minOccurs="0"/>
          </xs:sequence>
          <xs:attribute name="SourceName" type="xs:string" use="optional"/>
          <xs:attribute name="Type" type="TableTypeEnum" use="required"/>
          <xs:attribute name="Key" type="ColumnList" use="required"/>
          <xs:attribute name="Cached" type="TableCachedEnum" use="optional"/>
          <xs:attribute name="EventTimeCol" type="xs:NCName" use="optional"/>
          <xs:attribute name="LateDelivery" type="xs:boolean" use="optional"/>
        </xs:extension>
      </xs:complexContent>
    </xs:complexType>
    <xs:keyref name="TableJoinToRef" refer="TableId">
      <xs:selector xpath="."/>
      <xs:field xpath="@JoinTo"/>
    </xs:keyref>
  </xs:element>

  <xs:element name="JoinedTable">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="SubTable" maxOccurs="unbounded" minOccurs="1"/>
        <xs:element ref="Table-Reference" maxOccurs="unbounded" minOccurs="0"/>
      </xs:sequence>
      <xs:attribute name="Name" type="xs:NCName" use="required"/>
      <xs:attribute name="Type" type="TableTypeEnum" use="required"/>
      <xs:attribute name="Cached" type="TableCachedEnum" use="optional"/>
      <xs:attribute name="Key" type="ColumnList" use="required"/>
    </xs:complexType>
  </xs:element>

  <xs:element name="SubTable">
    <xs:complexType>
      <xs:complexContent>
        <xs:extension base="TableBaseType">
        </xs:extension>
      </xs:complexContent>
    </xs:complexType>
  </xs:element>

  <xs:element name="AdditionalTableName">
    <xs:complexType>
      <xs:attribute name="Name" type="xs:NCName" use="required"/>
    </xs:complexType>
  </xs:element>

  <xs:element name="AutoCreate-Historic-Table">
    <xs:complexType>
      <xs:attribute name="Name" type="xs:NCName" use="required"/>
    </xs:complexType>
  </xs:element>

  <xs:element name="Nexthink-NXQL-Template">
    <xs:complexType>
      <xs:simpleContent>
        <xs:extension base="xs:string">
          <xs:attribute name="Limit" type="xs:unsignedLong" use="optional"/>
        </xs:extension>
      </xs:simpleContent>
    </xs:complexType>
  </xs:element>

  <xs:element name="Matrix42-Pickup-Template">
    <xs:complexType>
      <xs:attribute name="PickupName" type="xs:string" use="required"/>
      <xs:attribute name="FilteredGroup" type="xs:positiveInteger" use="optional"/>
      <xs:attribute name="Sorting" type="M42PickupSortEnum" use="optional"/>
    </xs:complexType>
  </xs:element>

  <xs:element name="Matrix42-DataQueryItems-Template">
    <xs:complexType>
      <xs:attribute name="EntityClassName" type="xs:NCName" use="required"/>
      <xs:attribute name="EntityTypeNames" type="ColumnList" use="required"/>
      <xs:attribute name="OrderBy" type="xs:string " use="optional"/>
      <xs:attribute name="WhereExpression" type="xs:string " use="optional"/>
      <xs:attribute name="DataQueryName" type="xs:string" use="optional"/>
    </xs:complexType>
  </xs:element>

  <xs:element name="Table-Columns">
    <xs:complexType>
      <xs:sequence>
        <xs:choice maxOccurs="unbounded" minOccurs="1">
          <xs:element ref="Table-Column"/>
          <xs:element ref="Table-Column-Computation"/>
          <xs:element ref="Table-Column-NxtCompute"/>
          <xs:element ref="Table-Column-NxtCategory"/>
          <xs:element ref="Table-Column-NxtScore"/>
          <xs:element ref="Table-Column-NxtAction"/>
          <xs:element ref="Table-Column-NxtCampaign"/>
        </xs:choice>
      </xs:sequence>
    </xs:complexType>
    <xs:unique name="UniqueTableColumns">
      <xs:selector xpath="./Table-Column"/>
      <xs:field xpath="@Name"/>
    </xs:unique>
  </xs:element>

  <xs:complexType name="TableColumnType">
    <xs:complexContent>
      <xs:extension base="TableColumnBaseType">
        <xs:attribute name="SourceType" use="optional" type="StaticSourceTypeEnum"/>
        <xs:attribute name="SourceName" type="xs:string" use="optional"/>
        <xs:attribute name="SourceTable" type="xs:string" use="optional"/>
        <xs:attribute name="SourceJsonField" type="xs:string" use="optional"/>
        <xs:attribute name="SourceJsonColumn" type="xs:nonNegativeInteger" use="optional"/>
        <xs:attribute name="Aggregation" type="AggregationEnum" use="optional"/>
        <xs:attribute name="FillEmptyEntries" type="xs:boolean" use="optional"/>
        <xs:attribute name="LateDelivery" type="xs:boolean" use="optional"/>
        <xs:attribute name="IsWritable" type="xs:boolean" use="optional"/>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <xs:element name="Table-Column" type="TableColumnType"/>

  <xs:element name="Table-Column-NxtCompute" type="TableColumnType"/>

  <xs:element name="Table-Column-NxtCategory" type="TableColumnType"/>

  <xs:element name="Table-Column-NxtScore">
    <xs:complexType>
      <xs:complexContent>
        <xs:extension base="TableColumnBaseType">
          <xs:attribute name="ScoreName" type="xs:string" use="required"/>
          <xs:attribute name="ValueName" type="xs:string" use="required"/>
          <xs:attribute name="ScoreID" type="guid" use="optional"/>
          <xs:attribute name="ValueID" type="guid" use="optional"/>
          <xs:attribute name="Payload" type="xs:boolean" use="optional"/>
        </xs:extension>
      </xs:complexContent>
    </xs:complexType>
  </xs:element>

  <xs:element name="Table-Column-NxtAction">
    <xs:complexType>
      <xs:complexContent>
        <xs:extension base="TableColumnBaseType">
          <xs:attribute name="ActionName" type="xs:string" use="required"/>
          <xs:attribute name="ValueName" type="xs:string" use="required"/>
          <xs:attribute name="ActionID" type="guid" use="optional"/>
          <xs:attribute name="ValueID" type="guid" use="optional"/>
        </xs:extension>
      </xs:complexContent>
    </xs:complexType>
  </xs:element>

  <xs:element name="Table-Column-NxtCampaign">
    <xs:complexType>
      <xs:complexContent>
        <xs:extension base="TableColumnBaseType">
          <xs:attribute name="CampaignName" type="xs:string" use="required"/>
          <xs:attribute name="ValueName" type="xs:string" use="required"/>
          <xs:attribute name="CampaignID" type="guid" use="optional"/>
          <xs:attribute name="ValueID" type="xs:int" use="optional"/>
          <xs:attribute name="AsText" type="xs:boolean" use="optional"/>
        </xs:extension>
      </xs:complexContent>
    </xs:complexType>
  </xs:element>

  <xs:element name="Table-Column-Computation">
    <xs:complexType>
      <xs:complexContent>
        <xs:extension base="TableColumnBaseType">
          <xs:sequence>
            <xs:choice maxOccurs="1" minOccurs="1">
              <xs:element ref="Computation-LinearTransform"/>
              <xs:element ref="Computation-RegEx"/>
            </xs:choice>
          </xs:sequence>
        </xs:extension>
      </xs:complexContent>
    </xs:complexType>
  </xs:element>

  <xs:element name="Computation-LinearTransform">
    <xs:complexType>
      <xs:attribute name="Column" type="xs:NCName" use="required"/>
      <xs:attribute name="m" type="xs:float" use="required"/>
      <xs:attribute name="b" type="xs:float" use="required"/>
    </xs:complexType>
  </xs:element>

  <xs:element name="Computation-RegEx">
    <xs:complexType>
      <xs:attribute name="Column" type="xs:NCName" use="required"/>
      <xs:attribute name="RegExFilter" type="xs:string" use="required"/>
      <xs:attribute name="Replacement" type="xs:string" use="optional"/>
      <xs:attribute name="AutoTrim" type="xs:boolean" use="optional"/>
    </xs:complexType>
  </xs:element>

  <xs:element name="Table-Index">
    <xs:complexType>
      <xs:attribute name="Name" type="xs:NCName" use="required"/>
      <xs:attribute name="Columns" type="ColumnList" use="required"/>
      <xs:attribute name="Unique" type="xs:boolean" use="optional"/>
    </xs:complexType>
  </xs:element>

  <xs:element name="Table-Reference">
    <xs:complexType>
      <xs:attribute name="ForeignTable" type="xs:NCName" use="required"/>
      <xs:attribute name="Columns" type="ColumnList" use="required"/>
      <xs:attribute name="ForeignIndex" type="xs:NCName" use="optional"/>
    </xs:complexType>
  </xs:element>

</xs:schema>
