<?xml version="1.0" encoding="UTF-8"?>
<!--
  DAI Studio Extension Schema
  Namespace : https://dai.studio/schema/dai/1.0
  Prefix    : dai
  Version   : 1.0

  This schema describes the "dai:" extension vocabulary used in .dai documents.
  .dai files are valid BPMN 2.0 XML (http://www.omg.org/spec/BPMN/20100524/MODEL)
  extended with the types and attributes declared here.

  Extension mechanism
  ───────────────────
  BPMN 2.0 allows vendor extensions via <bpmn:extensionElements>. The dai:
  extension attributes are applied directly on BPMN elements using the dai:
  namespace prefix (e.g. <bpmn:task dai:stepType="context" />). Child elements
  such as <dai:workflowMeta> and <dai:aiTask> appear inside
  <bpmn:extensionElements>.

  The bpmn-moddle descriptor that drives the editor is the runtime authority
  for validation. This XSD is the canonical human- and tooling-readable
  declaration of the same vocabulary, served at the namespace URI so that XML
  tooling (validators, IDEs) can locate it automatically.

  Attribute groups
  ────────────────
  Because XSD cannot directly express "these attributes are attached to a
  foreign-namespace element type", the attributes are collected into named
  attributeGroups. Validators that perform namespace-aware attribute validation
  will find them here.

  Changelog
  ─────────
  1.0  Initial release — derived from DaiModdleDescriptor.json (both desktop
       and web variants); includes all attributes from both.
  1.0  (additive) dai:type on every flow element / artifact, dai:fields /
       dai:field for values of extension-catalog types, and dai:type on
       dai:taskTypeEntry. Existing documents remain valid.

  Type references
  ───────────────
  dai:type      Dot-path id of the element's type from the dai.studio
                extension catalog, e.g. dai:type="dai.types.business.objective".
                Canonical reference; see https://dai.studio/api/catalog.
  dai:stepType  Legacy slug (e.g. "objective"). Still written for types that
                have one and still accepted on its own. Readers should prefer
                dai:type when both are present.
-->
<xs:schema
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:dai="https://dai.studio/schema/dai/1.0"
  targetNamespace="https://dai.studio/schema/dai/1.0"
  elementFormDefault="qualified"
  attributeFormDefault="unqualified">

  <!-- ═══════════════════════════════════════════════════════════════════════
       ATTRIBUTE GROUPS
       Applied to BPMN elements via the dai: namespace prefix.
       ═══════════════════════════════════════════════════════════════════════ -->

  <!-- Attributes on every bpmn:FlowElement and bpmn:Artifact (TypedExtension) -->
  <xs:attributeGroup name="TypedExtensionAttrs">
    <!-- Dot-path type id: dai.types.<category>.<leaf> -->
    <xs:attribute name="type" type="dai:TypeId"/>
  </xs:attributeGroup>

  <xs:simpleType name="TypeId">
    <xs:restriction base="xs:string">
      <xs:pattern value="dai\.types\.[a-z0-9]+(-[a-z0-9]+)*\.[a-z0-9]+(-[a-z0-9]+)*"/>
    </xs:restriction>
  </xs:simpleType>

  <!-- Attributes on bpmn:Task (and most task-like elements) -->
  <xs:attributeGroup name="TaskExtensionAttrs">
    <!-- Core -->
    <xs:attribute name="stepType"  type="xs:string"/>
    <xs:attribute name="notes"     type="xs:string"/>
    <xs:attribute name="code"      type="xs:string"/>
    <xs:attribute name="version"   type="xs:string"/>

    <!-- Timer / Condition -->
    <xs:attribute name="timerDefinition" type="xs:string"/>
    <xs:attribute name="condition"       type="xs:string"/>

    <!-- Risk -->
    <xs:attribute name="probability" type="xs:string"/>
    <xs:attribute name="impact"      type="xs:string"/>
    <xs:attribute name="mitigation"  type="xs:string"/>
    <xs:attribute name="owner"       type="xs:string"/>

    <!-- Decision record / ADR -->
    <xs:attribute name="status"       type="xs:string"/>
    <xs:attribute name="context"      type="xs:string"/>
    <xs:attribute name="decision"     type="xs:string"/>
    <xs:attribute name="consequences" type="xs:string"/>

    <!-- KPI / Metric -->
    <xs:attribute name="target"    type="xs:string"/>
    <xs:attribute name="unit"      type="xs:string"/>
    <xs:attribute name="formula"   type="xs:string"/>
    <xs:attribute name="frequency" type="xs:string"/>

    <!-- API / Integration -->
    <xs:attribute name="method"       type="xs:string"/>
    <xs:attribute name="path"         type="xs:string"/>
    <xs:attribute name="authType"     type="xs:string"/>
    <xs:attribute name="requestBody"  type="xs:string"/>
    <xs:attribute name="responseBody" type="xs:string"/>

    <!-- AI prompting -->
    <xs:attribute name="systemPrompt" type="xs:string"/>
    <xs:attribute name="userPrompt"   type="xs:string"/>
    <xs:attribute name="outputFormat" type="xs:string"/>

    <!-- OKR / Strategy -->
    <xs:attribute name="timeframe"          type="xs:string"/>
    <xs:attribute name="keyResults"         type="xs:string"/>
    <xs:attribute name="interest"           type="xs:string"/>
    <xs:attribute name="influence"          type="xs:string"/>
    <xs:attribute name="engagementStrategy" type="xs:string"/>
    <xs:attribute name="priority"           type="xs:string"/>
    <xs:attribute name="source"             type="xs:string"/>
    <xs:attribute name="acceptanceCriteria" type="xs:string"/>
    <xs:attribute name="basis"              type="xs:string"/>
    <xs:attribute name="ifWrong"            type="xs:string"/>

    <!-- Experiment / Hypothesis -->
    <xs:attribute name="valueStatement" type="xs:string"/>
    <xs:attribute name="theme"          type="xs:string"/>
    <xs:attribute name="goal"           type="xs:string"/>
    <xs:attribute name="steps"          type="xs:string"/>
    <xs:attribute name="painPoints"     type="xs:string"/>
    <xs:attribute name="opportunities"  type="xs:string"/>
    <xs:attribute name="hypothesis"     type="xs:string"/>
    <xs:attribute name="audience"       type="xs:string"/>
    <xs:attribute name="successMetric"  type="xs:string"/>
    <xs:attribute name="result"         type="xs:string"/>

    <!-- Persona -->
    <xs:attribute name="role"         type="xs:string"/>
    <xs:attribute name="age"          type="xs:string"/>
    <xs:attribute name="goals"        type="xs:string"/>
    <xs:attribute name="frustrations" type="xs:string"/>
    <xs:attribute name="behaviours"   type="xs:string"/>
    <xs:attribute name="quote"        type="xs:string"/>
    <xs:attribute name="storyPoints"  type="xs:string"/>

    <!-- Architecture / Infrastructure -->
    <xs:attribute name="baseUrl"        type="xs:string"/>
    <xs:attribute name="engine"         type="xs:string"/>
    <xs:attribute name="host"           type="xs:string"/>
    <xs:attribute name="accessPattern"  type="xs:string"/>
    <xs:attribute name="componentType"  type="xs:string"/>
    <xs:attribute name="propsInterface" type="xs:string"/>
    <xs:attribute name="producer"       type="xs:string"/>
    <xs:attribute name="consumers"      type="xs:string"/>
    <xs:attribute name="payloadSchema"  type="xs:string"/>
    <xs:attribute name="broker"         type="xs:string"/>

    <!-- Dependency management -->
    <xs:attribute name="package"        type="xs:string"/>
    <xs:attribute name="dependencyType" type="xs:string"/>
    <xs:attribute name="license"        type="xs:string"/>
    <xs:attribute name="purpose"        type="xs:string"/>

    <!-- Issue / Bug tracking -->
    <xs:attribute name="severity"         type="xs:string"/>
    <xs:attribute name="assignee"         type="xs:string"/>
    <xs:attribute name="issueType"        type="xs:string"/>
    <xs:attribute name="stepsToReproduce" type="xs:string"/>

    <!-- Model / ML -->
    <xs:attribute name="modelFormat" type="xs:string"/>

    <!-- Test -->
    <xs:attribute name="testType"   type="xs:string"/>
    <xs:attribute name="testStatus" type="xs:string"/>
    <xs:attribute name="testSteps"  type="xs:string"/>

    <!-- API response -->
    <xs:attribute name="statusCode" type="xs:string"/>

    <!-- Rule / Content -->
    <xs:attribute name="format"       type="xs:string"/>
    <xs:attribute name="scope"        type="xs:string"/>
    <xs:attribute name="content"      type="xs:string"/>
    <xs:attribute name="rulePriority" type="xs:string"/>
    <xs:attribute name="enforcement"  type="xs:string"/>
    <xs:attribute name="trigger"      type="xs:string"/>
    <xs:attribute name="examples"     type="xs:string"/>
    <xs:attribute name="language"     type="xs:string"/>
    <xs:attribute name="returns"      type="xs:string"/>
    <xs:attribute name="temperature"  type="xs:string"/>

    <!-- Image (desktop variant) -->
    <xs:attribute name="imageSource" type="xs:string"/>
    <xs:attribute name="alt"         type="xs:string"/>

    <!-- Observed in production documents, not in descriptor -->
    <xs:attribute name="expectedResult" type="xs:string"/>
  </xs:attributeGroup>

  <!-- Attributes on bpmn:SubProcess (SubProcessExtension) -->
  <xs:attributeGroup name="SubProcessExtensionAttrs">
    <xs:attribute name="notes" type="xs:string"/>
  </xs:attributeGroup>

  <!-- Attributes on bpmn:SubProcess (AgentExtension — web variant) -->
  <xs:attributeGroup name="AgentExtensionAttrs">
    <xs:attribute name="stepType"      type="xs:string"/>
    <xs:attribute name="agentRole"     type="xs:string"/>
    <xs:attribute name="defaultModel"  type="xs:string"/>
    <xs:attribute name="agentType"     type="xs:string"/>
    <xs:attribute name="memoryType"    type="xs:string"/>
    <xs:attribute name="maxIterations" type="xs:string"/>
    <xs:attribute name="stopCondition" type="xs:string"/>
    <xs:attribute name="outputSchema"  type="xs:string"/>
  </xs:attributeGroup>

  <!-- Attributes on bpmn:Group (GroupExtension) -->
  <xs:attributeGroup name="GroupExtensionAttrs">
    <xs:attribute name="notes" type="xs:string"/>
  </xs:attributeGroup>

  <!-- Attributes on bpmn:StartEvent -->
  <xs:attributeGroup name="StartEventExtensionAttrs">
    <xs:attribute name="notes" type="xs:string"/>
  </xs:attributeGroup>

  <!-- Attributes on bpmn:EndEvent -->
  <xs:attributeGroup name="EndEventExtensionAttrs">
    <xs:attribute name="notes" type="xs:string"/>
  </xs:attributeGroup>

  <!-- Attributes on bpmn:IntermediateThrowEvent -->
  <xs:attributeGroup name="IntermediateThrowEventExtensionAttrs">
    <xs:attribute name="notes" type="xs:string"/>
  </xs:attributeGroup>

  <!-- Attributes on bpmn:IntermediateCatchEvent -->
  <xs:attributeGroup name="IntermediateCatchEventExtensionAttrs">
    <xs:attribute name="notes" type="xs:string"/>
  </xs:attributeGroup>

  <!-- Attributes on bpmn:ExclusiveGateway -->
  <xs:attributeGroup name="ExclusiveGatewayExtensionAttrs">
    <xs:attribute name="notes" type="xs:string"/>
  </xs:attributeGroup>

  <!-- Attributes on bpmn:ParallelGateway -->
  <xs:attributeGroup name="ParallelGatewayExtensionAttrs">
    <xs:attribute name="notes" type="xs:string"/>
  </xs:attributeGroup>

  <!-- Attributes on bpmn:DataStoreReference -->
  <xs:attributeGroup name="DataStoreReferenceExtensionAttrs">
    <xs:attribute name="notes" type="xs:string"/>
  </xs:attributeGroup>


  <!-- ═══════════════════════════════════════════════════════════════════════
       ELEMENT DECLARATIONS
       These elements appear inside <bpmn:extensionElements>.
       ═══════════════════════════════════════════════════════════════════════ -->

  <!--
    dai:workflowMeta
    Appears in: bpmn:Process, bpmn:Collaboration
    Carries diagram-level metadata and an optional list of task type entries.

    Example:
      <bpmn:extensionElements>
        <dai:workflowMeta description="..." defaultModel="gpt-4o" version="0.1.0">
          <dai:taskTypeEntry stepType="context" description="..." />
        </dai:workflowMeta>
      </bpmn:extensionElements>
  -->
  <xs:element name="workflowMeta">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="dai:taskTypeEntry" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
      <xs:attribute name="description"     type="xs:string"/>
      <xs:attribute name="defaultModel"    type="xs:string"/>
      <xs:attribute name="targetLanguage"  type="xs:string"/>
      <xs:attribute name="targetFramework" type="xs:string"/>
      <xs:attribute name="version"         type="xs:string"/>
    </xs:complexType>
  </xs:element>

  <!--
    dai:taskTypeEntry
    Child of dai:workflowMeta. One entry per step type used in the diagram.
  -->
  <xs:element name="taskTypeEntry">
    <xs:complexType>
      <xs:attribute name="type"        type="dai:TypeId"/>
      <xs:attribute name="stepType"    type="xs:string"/>
      <xs:attribute name="description" type="xs:string"/>
    </xs:complexType>
  </xs:element>

  <!--
    dai:aiTask
    Appears in: bpmn:ServiceTask, bpmn:Task
    Carries AI model configuration and prompt content.

    Example:
      <bpmn:extensionElements>
        <dai:aiTask stepType="ai" model="gpt-4o" temperature="0.7" maxTokens="2048">
          <dai:prompt>Summarise the following input: ...</dai:prompt>
          <dai:outputSchema>{ "summary": "string" }</dai:outputSchema>
        </dai:aiTask>
      </bpmn:extensionElements>
  -->
  <xs:element name="aiTask">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="dai:prompt"       minOccurs="0" maxOccurs="1"/>
        <xs:element ref="dai:outputSchema" minOccurs="0" maxOccurs="1"/>
      </xs:sequence>
      <xs:attribute name="stepType"     type="xs:string" default="ai"/>
      <xs:attribute name="model"        type="xs:string"/>
      <xs:attribute name="temperature"  type="xs:string"/>
      <xs:attribute name="maxTokens"    type="xs:integer"/>
      <xs:attribute name="outputFormat" type="xs:string"/>
    </xs:complexType>
  </xs:element>

  <!--
    dai:prompt
    Child of dai:aiTask. Body text is the prompt content.
  -->
  <xs:element name="prompt">
    <xs:complexType>
      <xs:simpleContent>
        <xs:extension base="xs:string"/>
      </xs:simpleContent>
    </xs:complexType>
  </xs:element>

  <!--
    dai:outputSchema
    Child of dai:aiTask. Body text is a JSON schema string.
  -->
  <xs:element name="outputSchema">
    <xs:complexType>
      <xs:simpleContent>
        <xs:extension base="xs:string"/>
      </xs:simpleContent>
    </xs:complexType>
  </xs:element>

  <!--
    dai:humanReview
    Appears in: bpmn:UserTask
    Configures a human review / approval step.
  -->
  <xs:element name="humanReview">
    <xs:complexType>
      <xs:attribute name="instructions" type="xs:string"/>
      <xs:attribute name="approveLabel" type="xs:string"/>
      <xs:attribute name="rejectLabel"  type="xs:string"/>
    </xs:complexType>
  </xs:element>

  <!--
    dai:validation
    Appears in: bpmn:BusinessRuleTask
    Configures a validation step.
  -->
  <xs:element name="validation">
    <xs:complexType>
      <xs:attribute name="validationType" type="xs:string"/>
      <xs:attribute name="rules"          type="xs:string"/>
      <xs:attribute name="failBehavior"   type="xs:string"/>
    </xs:complexType>
  </xs:element>

  <!--
    dai:toolExecution
    Appears in: bpmn:ServiceTask, bpmn:ScriptTask
    Configures a tool / command execution step.
  -->
  <xs:element name="toolExecution">
    <xs:complexType>
      <xs:attribute name="toolType"   type="xs:string"/>
      <xs:attribute name="command"    type="xs:string"/>
      <xs:attribute name="args"       type="xs:string"/>
      <xs:attribute name="workingDir" type="xs:string"/>
    </xs:complexType>
  </xs:element>

  <!--
    dai:conditionLabel
    Appears in: bpmn:SequenceFlow
    Attaches a human-readable label and optional expression to a flow.
  -->
  <xs:element name="conditionLabel">
    <xs:complexType>
      <xs:attribute name="label"      type="xs:string"/>
      <xs:attribute name="expression" type="xs:string"/>
    </xs:complexType>
  </xs:element>

  <!--
    dai:properties
    Appears in: bpmn:Task, bpmn:SubProcess
    Container for typed model field definitions.
  -->
  <xs:element name="properties">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="dai:property" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <!--
    dai:property
    Child of dai:properties. A single typed field definition.
  -->
  <xs:element name="property">
    <xs:complexType>
      <xs:attribute name="name" type="xs:string"/>
      <xs:attribute name="type" type="xs:string"/>
    </xs:complexType>
  </xs:element>

  <!--
    dai:fields
    Appears in: <bpmn:extensionElements> of any flow element / artifact.
    Holds the values of form fields that have no dedicated dai: attribute
    (storage "field" in the extension catalog). One dai:field per key; the
    value is the element text, so multi-line code / markdown is preserved.

    Example:
      <bpmn:task id="Task_1" name="Checkout API" dai:type="dai.types.software.repository" dai:stepType="repository">
        <bpmn:extensionElements>
          <dai:fields>
            <dai:field key="url">https://github.com/acme/checkout</dai:field>
            <dai:field key="branch">main</dai:field>
          </dai:fields>
        </bpmn:extensionElements>
      </bpmn:task>
  -->
  <xs:element name="fields">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="dai:field" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <!--
    dai:field
    Child of dai:fields. `key` is the form field key; body text is the value.
  -->
  <xs:element name="field">
    <xs:complexType>
      <xs:simpleContent>
        <xs:extension base="xs:string">
          <xs:attribute name="key" type="xs:string" use="required"/>
        </xs:extension>
      </xs:simpleContent>
    </xs:complexType>
  </xs:element>

</xs:schema>
