<xs:schema xmlns:w="http://www.appian.com/workflow/types/2024">
    <xs:complexType name="workflow">
        <xs:annotation>
            <xs:documentation>
                Root workflow element containing swimlanes and nodes that define a complete business process.
                Every workflow must have at least one start node and at least one end node.
                All nodes must be reachable from at least one start node through edgeLabels connections.
            </xs:documentation>
        </xs:annotation>
        <xs:sequence>
            <xs:element name="swimlanes" type="w:swimlane" minOccurs="1" maxOccurs="unbounded" />
            <xs:element name="nodes" type="w:node" minOccurs="2" maxOccurs="unbounded" />
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="swimlane">
        <xs:sequence>
            <xs:element name="id" type="xs:string" />
            <xs:element name="label" type="xs:string" />
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="node">
        <xs:annotation>
            <xs:documentation>
                Workflow node representing an activity, event, or gateway.
                
                VALIDATION RULES:
                - All swimlaneId values must reference valid swimlane ids
                - All targetNodeId values in edgeLabels must reference existing nodes
                - Start nodes: Every start node's swimlaneId MUST equal the swimlaneId of the first node in its edgeLabels array.
                - End nodes: Every end node's swimlaneId MUST equal the swimlaneId of the previous node.
                - Include edgeLabels for all connections; use empty label string for unlabeled connections
            </xs:documentation>
        </xs:annotation>
        <xs:sequence>
            <xs:element name="id" type="xs:string" />
            <xs:element name="label" type="xs:string" />
            <xs:element name="type" type="w:nodeType" />
            <xs:element name="swimlaneId" type="xs:string">
                <xs:annotation>
                    <xs:documentation>
                        Reference to a valid swimlane id. For start nodes, this must match the swimlaneId 
                        of the first node in the edgeLabels. For end nodes, this must match the swimlaneId 
                        of the node that connects to this end node.
                    </xs:documentation>
                </xs:annotation>
            </xs:element>
            <xs:element name="index" type="xs:int" />
            <xs:element name="description" type="xs:string" minOccurs="0" />
            <xs:element name="activityType" type="w:activityType" minOccurs="0" />
            <xs:element name="edgeLabels" type="w:edgeLabel" minOccurs="0" maxOccurs="unbounded">
                <xs:annotation>
                    <xs:documentation>
                        Labels for edges/connections from this node. Defines all outgoing connections.
                        For unlabeled connections, use empty string for label. End nodes have no edgeLabels.
                    </xs:documentation>
                </xs:annotation>
            </xs:element>
        </xs:sequence>

        <xs:assert test="
            (type = 'userActivity' and activityType = 'userTask') or
            (type = 'automatedActivity' and (activityType = 'integration' or activityType = 'general' or activityType = 'rpa' or activityType = 'ai')) or
            (type = 'start' or type = 'end' or type = 'exclusiveGateway' or type = 'parallelGateway')
        ">
            <xs:annotation>
                <xs:documentation>
                    Valid type/activityType combinations:
                    - userActivity must have activityType = 'userTask'
                    - automatedActivity must have activityType = 'integration', 'general', 'rpa', or 'ai'
                    - start, end, exclusiveGateway, parallelGateway do not require activityType
                </xs:documentation>
            </xs:annotation>
        </xs:assert>
    </xs:complexType>

    <xs:complexType name="edgeLabel">
        <xs:sequence>
            <xs:element name="targetNodeId" type="xs:string" />
            <xs:element name="label" type="xs:string" />
        </xs:sequence>
    </xs:complexType>

    <xs:simpleType name="nodeType">
        <xs:annotation>
            <xs:documentation>
                Node types: start (required), end (required), userActivity (human tasks),
                automatedActivity (system tasks), exclusiveGateway (either/or), parallelGateway (concurrent)
                
                GATEWAY BEHAVIOR:
                - exclusiveGateway (XOR): 1 or more paths can enter, 1 or more paths can exit, but only ONE path gets executed. 
                  MUST have labeled edgeLabels to indicate decision criteria (e.g., "Yes"/"No", "Approved"/"Rejected").
                  Gateway exits must connect to new business processes or forms, never to activities that record the gateway decision.
                - parallelGateway (AND): 1 or more paths can enter, 1 or more paths can exit, ALL paths get executed concurrently.
            </xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:string">
            <xs:enumeration value="start" />
            <xs:enumeration value="end" />
            <xs:enumeration value="userActivity" />
            <xs:enumeration value="automatedActivity" />
            <xs:enumeration value="exclusiveGateway" />
            <xs:enumeration value="parallelGateway" />
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="activityType">
        <xs:annotation>
            <xs:documentation>
                Activity subtypes for categorizing work:
                - userTask: Complete form interaction from opening to final submission, including all input, validation, and processing steps
                - integration: External system integrations
                - general: Basic automation
                - rpa: Robotic process automation
                - ai: AI-powered automation
            </xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:string">
            <xs:enumeration value="userTask" />
            <xs:enumeration value="integration" />
            <xs:enumeration value="general" />
            <xs:enumeration value="rpa" />
            <xs:enumeration value="ai" />
        </xs:restriction>
    </xs:simpleType>
</xs:schema>
