<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL"
                  xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI"
                  xmlns:dc="http://www.omg.org/spec/DD/20100524/DC"
                  xmlns:di="http://www.omg.org/spec/DD/20100524/DI"
                  xmlns:camunda="http://camunda.org/schema/1.0/bpmn"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  id="Definitions_Consent"
                  targetNamespace="http://breakpilot.de/bpmn/consent">

  <bpmn:process id="ConsentDocumentProcess" name="Consent-Dokument Workflow" isExecutable="true">

    <!-- Start Event -->
    <bpmn:startEvent id="start" name="Dokument erstellt">
      <bpmn:outgoing>flow_to_edit</bpmn:outgoing>
    </bpmn:startEvent>

    <!-- User Task: Dokument bearbeiten -->
    <bpmn:userTask id="edit_document" name="Dokument bearbeiten" camunda:assignee="${authorId}">
      <bpmn:extensionElements>
        <camunda:formData>
          <camunda:formField id="title" label="Titel" type="string" />
          <camunda:formField id="type" label="Dokumenttyp" type="enum">
            <camunda:value id="terms" name="AGB" />
            <camunda:value id="privacy" name="Datenschutzerklaerung" />
            <camunda:value id="cookies" name="Cookie-Richtlinie" />
            <camunda:value id="consent_form" name="Einwilligungserklaerung" />
          </camunda:formField>
          <camunda:formField id="content" label="Inhalt" type="string" />
          <camunda:formField id="version" label="Version" type="string" />
        </camunda:formData>
      </bpmn:extensionElements>
      <bpmn:incoming>flow_to_edit</bpmn:incoming>
      <bpmn:incoming>flow_rejected_to_edit</bpmn:incoming>
      <bpmn:outgoing>flow_to_review</bpmn:outgoing>
    </bpmn:userTask>

    <!-- User Task: DSB Review -->
    <bpmn:userTask id="dsb_review" name="DSB Pruefung" camunda:candidateGroups="data_protection_officer">
      <bpmn:extensionElements>
        <camunda:formData>
          <camunda:formField id="approved" label="Genehmigt" type="boolean" />
          <camunda:formField id="comments" label="Kommentare" type="string" />
          <camunda:formField id="legalCheck" label="Rechtliche Pruefung OK" type="boolean" />
        </camunda:formData>
      </bpmn:extensionElements>
      <bpmn:incoming>flow_to_review</bpmn:incoming>
      <bpmn:outgoing>flow_to_approval_gateway</bpmn:outgoing>
    </bpmn:userTask>

    <!-- Gateway: Genehmigt? -->
    <bpmn:exclusiveGateway id="approval_gateway" name="Genehmigt?">
      <bpmn:incoming>flow_to_approval_gateway</bpmn:incoming>
      <bpmn:outgoing>flow_approved</bpmn:outgoing>
      <bpmn:outgoing>flow_rejected</bpmn:outgoing>
    </bpmn:exclusiveGateway>

    <!-- Service Task: Veroeffentlichen -->
    <bpmn:serviceTask id="publish_document" name="Dokument veroeffentlichen" camunda:delegateExpression="${publishConsentDocumentDelegate}">
      <bpmn:incoming>flow_approved</bpmn:incoming>
      <bpmn:outgoing>flow_to_notify</bpmn:outgoing>
    </bpmn:serviceTask>

    <!-- Service Task: Benutzer benachrichtigen -->
    <bpmn:serviceTask id="notify_users" name="Benutzer benachrichtigen" camunda:delegateExpression="${notifyUsersDelegate}">
      <bpmn:incoming>flow_to_notify</bpmn:incoming>
      <bpmn:outgoing>flow_to_collect_consent</bpmn:outgoing>
    </bpmn:serviceTask>

    <!-- User Task: Consent sammeln (wartet auf Benutzer-Zustimmungen) -->
    <bpmn:receiveTask id="collect_consent" name="Auf Zustimmungen warten">
      <bpmn:incoming>flow_to_collect_consent</bpmn:incoming>
      <bpmn:outgoing>flow_to_check_deadline</bpmn:outgoing>
    </bpmn:receiveTask>

    <!-- Boundary Timer: Deadline -->
    <bpmn:boundaryEvent id="consent_deadline" attachedToRef="collect_consent" cancelActivity="false">
      <bpmn:timerEventDefinition>
        <bpmn:timeDuration>P${consentDeadlineDays}D</bpmn:timeDuration>
      </bpmn:timerEventDefinition>
      <bpmn:outgoing>flow_to_reminder</bpmn:outgoing>
    </bpmn:boundaryEvent>

    <!-- Service Task: Reminder senden -->
    <bpmn:serviceTask id="send_reminder" name="Reminder senden" camunda:delegateExpression="${sendConsentReminderDelegate}">
      <bpmn:incoming>flow_to_reminder</bpmn:incoming>
      <bpmn:outgoing>flow_back_to_collect</bpmn:outgoing>
    </bpmn:serviceTask>

    <!-- Service Task: Consent-Status pruefen -->
    <bpmn:serviceTask id="check_consent_status" name="Consent-Status pruefen" camunda:delegateExpression="${checkConsentStatusDelegate}">
      <bpmn:incoming>flow_to_check_deadline</bpmn:incoming>
      <bpmn:outgoing>flow_to_active</bpmn:outgoing>
    </bpmn:serviceTask>

    <!-- Intermediate Event: Dokument aktiv -->
    <bpmn:intermediateThrowEvent id="document_active" name="Dokument aktiv">
      <bpmn:incoming>flow_to_active</bpmn:incoming>
      <bpmn:outgoing>flow_to_monitor</bpmn:outgoing>
    </bpmn:intermediateThrowEvent>

    <!-- Sub-Process: Monitoring -->
    <bpmn:subProcess id="monitoring_subprocess" name="Consent Monitoring">
      <bpmn:incoming>flow_to_monitor</bpmn:incoming>
      <bpmn:outgoing>flow_to_archive</bpmn:outgoing>

      <bpmn:startEvent id="monitoring_start" />

      <!-- Event-based Gateway: Warte auf Events -->
      <bpmn:eventBasedGateway id="event_gateway">
        <bpmn:incoming>flow_from_monitoring_start</bpmn:incoming>
        <bpmn:outgoing>flow_to_renewal_timer</bpmn:outgoing>
        <bpmn:outgoing>flow_to_supersede_event</bpmn:outgoing>
      </bpmn:eventBasedGateway>

      <!-- Timer: Jaehrliche Erneuerung -->
      <bpmn:intermediateCatchEvent id="renewal_timer" name="Erneuerungsdatum">
        <bpmn:incoming>flow_to_renewal_timer</bpmn:incoming>
        <bpmn:outgoing>flow_to_renewal_task</bpmn:outgoing>
        <bpmn:timerEventDefinition>
          <bpmn:timeDuration>P1Y</bpmn:timeDuration>
        </bpmn:timerEventDefinition>
      </bpmn:intermediateCatchEvent>

      <!-- Message: Dokument ersetzt -->
      <bpmn:intermediateCatchEvent id="supersede_event" name="Neue Version">
        <bpmn:incoming>flow_to_supersede_event</bpmn:incoming>
        <bpmn:outgoing>flow_to_monitoring_end</bpmn:outgoing>
        <bpmn:messageEventDefinition messageRef="Message_Supersede" />
      </bpmn:intermediateCatchEvent>

      <bpmn:serviceTask id="trigger_renewal" name="Erneuerung anfordern" camunda:delegateExpression="${triggerRenewalDelegate}">
        <bpmn:incoming>flow_to_renewal_task</bpmn:incoming>
        <bpmn:outgoing>flow_back_to_gateway</bpmn:outgoing>
      </bpmn:serviceTask>

      <bpmn:endEvent id="monitoring_end" />
    </bpmn:subProcess>

    <!-- Service Task: Archivieren -->
    <bpmn:serviceTask id="archive_document" name="Dokument archivieren" camunda:delegateExpression="${archiveDocumentDelegate}">
      <bpmn:incoming>flow_to_archive</bpmn:incoming>
      <bpmn:outgoing>flow_to_end</bpmn:outgoing>
    </bpmn:serviceTask>

    <!-- End Event -->
    <bpmn:endEvent id="end" name="Workflow beendet">
      <bpmn:incoming>flow_to_end</bpmn:incoming>
    </bpmn:endEvent>

    <!-- Sequence Flows -->
    <bpmn:sequenceFlow id="flow_to_edit" sourceRef="start" targetRef="edit_document" />
    <bpmn:sequenceFlow id="flow_to_review" sourceRef="edit_document" targetRef="dsb_review" />
    <bpmn:sequenceFlow id="flow_to_approval_gateway" sourceRef="dsb_review" targetRef="approval_gateway" />
    <bpmn:sequenceFlow id="flow_approved" sourceRef="approval_gateway" targetRef="publish_document">
      <bpmn:conditionExpression xsi:type="bpmn:tFormalExpression">${approved == true}</bpmn:conditionExpression>
    </bpmn:sequenceFlow>
    <bpmn:sequenceFlow id="flow_rejected" sourceRef="approval_gateway" targetRef="edit_document">
      <bpmn:conditionExpression xsi:type="bpmn:tFormalExpression">${approved == false}</bpmn:conditionExpression>
    </bpmn:sequenceFlow>
    <bpmn:sequenceFlow id="flow_rejected_to_edit" sourceRef="approval_gateway" targetRef="edit_document" />
    <bpmn:sequenceFlow id="flow_to_notify" sourceRef="publish_document" targetRef="notify_users" />
    <bpmn:sequenceFlow id="flow_to_collect_consent" sourceRef="notify_users" targetRef="collect_consent" />
    <bpmn:sequenceFlow id="flow_to_reminder" sourceRef="consent_deadline" targetRef="send_reminder" />
    <bpmn:sequenceFlow id="flow_to_check_deadline" sourceRef="collect_consent" targetRef="check_consent_status" />
    <bpmn:sequenceFlow id="flow_to_active" sourceRef="check_consent_status" targetRef="document_active" />
    <bpmn:sequenceFlow id="flow_to_monitor" sourceRef="document_active" targetRef="monitoring_subprocess" />
    <bpmn:sequenceFlow id="flow_to_archive" sourceRef="monitoring_subprocess" targetRef="archive_document" />
    <bpmn:sequenceFlow id="flow_to_end" sourceRef="archive_document" targetRef="end" />

  </bpmn:process>

  <!-- Messages -->
  <bpmn:message id="Message_Supersede" name="DocumentSuperseded" />

  <!-- BPMN Diagram -->
  <bpmndi:BPMNDiagram id="BPMNDiagram_1">
    <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="ConsentDocumentProcess">
      <bpmndi:BPMNShape id="start_di" bpmnElement="start">
        <dc:Bounds x="152" y="102" width="36" height="36" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="edit_document_di" bpmnElement="edit_document">
        <dc:Bounds x="240" y="80" width="100" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="dsb_review_di" bpmnElement="dsb_review">
        <dc:Bounds x="390" y="80" width="100" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="approval_gateway_di" bpmnElement="approval_gateway" isMarkerVisible="true">
        <dc:Bounds x="545" y="95" width="50" height="50" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="publish_document_di" bpmnElement="publish_document">
        <dc:Bounds x="650" y="80" width="100" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="notify_users_di" bpmnElement="notify_users">
        <dc:Bounds x="800" y="80" width="100" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="collect_consent_di" bpmnElement="collect_consent">
        <dc:Bounds x="950" y="80" width="100" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="end_di" bpmnElement="end">
        <dc:Bounds x="1502" y="102" width="36" height="36" />
      </bpmndi:BPMNShape>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>

</bpmn:definitions>
