<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="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"
             targetNamespace="http://bpmn.io/schema/bpmn"
             id="complex-loop-definitions">

  <process id="complex-loop-process" name="Proceso de Control de Calidad con Revisión" isExecutable="false">

    <startEvent id="start" name="Producto listo para QA">
      <outgoing>flow_start_inspeccion</outgoing>
    </startEvent>

    <task id="task_preparar" name="Preparar lote para inspección">
      <incoming>flow_start_inspeccion</incoming>
      <outgoing>flow_preparar_inspeccion</outgoing>
    </task>

    <task id="task_inspeccion" name="Inspección visual y funcional">
      <incoming>flow_preparar_inspeccion</incoming>
      <incoming>flow_correc_inspeccion</incoming>
      <outgoing>flow_inspeccion_gw</outgoing>
    </task>

    <!-- Gateway XOR: resultado de inspección -->
    <exclusiveGateway id="gw_resultado" name="¿Pasa control?">
      <incoming>flow_inspeccion_gw</incoming>
      <outgoing>flow_gw_aprobado</outgoing>
      <outgoing>flow_gw_obs_menores</outgoing>
      <outgoing>flow_gw_rechazo</outgoing>
    </exclusiveGateway>

    <task id="task_documentar" name="Documentar aprobación">
      <incoming>flow_gw_aprobado</incoming>
      <outgoing>flow_documentar_entrega</outgoing>
    </task>

    <!-- Loop: correcciones menores → volver a inspección -->
    <task id="task_correcciones" name="Realizar correcciones menores">
      <incoming>flow_gw_obs_menores</incoming>
      <outgoing>flow_correc_inspeccion</outgoing>
    </task>

    <!-- Gateway XOR: rechazo total -->
    <task id="task_rechazo" name="Separar producto rechazado">
      <incoming>flow_gw_rechazo</incoming>
      <outgoing>flow_rechazo_informe</outgoing>
    </task>

    <task id="task_informe_rechazo" name="Emitir informe de no conformidad">
      <incoming>flow_rechazo_informe</incoming>
      <outgoing>flow_informe_merge</outgoing>
    </task>

    <task id="task_entrega" name="Preparar entrega al cliente">
      <incoming>flow_documentar_entrega</incoming>
      <outgoing>flow_entrega_certificado</outgoing>
    </task>

    <task id="task_certificado" name="Emitir certificado de calidad">
      <incoming>flow_entrega_certificado</incoming>
      <outgoing>flow_cert_merge</outgoing>
    </task>

    <!-- Convergencia final -->
    <exclusiveGateway id="gw_merge" name="Merge final">
      <incoming>flow_cert_merge</incoming>
      <incoming>flow_informe_merge</incoming>
      <outgoing>flow_merge_registro</outgoing>
    </exclusiveGateway>

    <task id="task_registro" name="Registrar resultado en sistema">
      <incoming>flow_merge_registro</incoming>
      <outgoing>flow_registro_end</outgoing>
    </task>

    <endEvent id="end" name="Control de calidad finalizado">
      <incoming>flow_registro_end</incoming>
    </endEvent>

    <!-- Sequence Flows -->
    <sequenceFlow id="flow_start_inspeccion" sourceRef="start" targetRef="task_preparar"/>
    <sequenceFlow id="flow_preparar_inspeccion" sourceRef="task_preparar" targetRef="task_inspeccion"/>
    <sequenceFlow id="flow_inspeccion_gw" sourceRef="task_inspeccion" targetRef="gw_resultado"/>
    <sequenceFlow id="flow_gw_aprobado" name="Aprobado" sourceRef="gw_resultado" targetRef="task_documentar"/>
    <sequenceFlow id="flow_gw_obs_menores" name="Obs. menores" sourceRef="gw_resultado" targetRef="task_correcciones"/>
    <sequenceFlow id="flow_gw_rechazo" name="Rechazado" sourceRef="gw_resultado" targetRef="task_rechazo"/>
    <!-- LOOP: correcciones vuelven a inspección -->
    <sequenceFlow id="flow_correc_inspeccion" sourceRef="task_correcciones" targetRef="task_inspeccion"/>
    <sequenceFlow id="flow_rechazo_informe" sourceRef="task_rechazo" targetRef="task_informe_rechazo"/>
    <sequenceFlow id="flow_informe_merge" sourceRef="task_informe_rechazo" targetRef="gw_merge"/>
    <sequenceFlow id="flow_documentar_entrega" sourceRef="task_documentar" targetRef="task_entrega"/>
    <sequenceFlow id="flow_entrega_certificado" sourceRef="task_entrega" targetRef="task_certificado"/>
    <sequenceFlow id="flow_cert_merge" sourceRef="task_certificado" targetRef="gw_merge"/>
    <sequenceFlow id="flow_merge_registro" sourceRef="gw_merge" targetRef="task_registro"/>
    <sequenceFlow id="flow_registro_end" sourceRef="task_registro" targetRef="end"/>

  </process>

  <bpmndi:BPMNDiagram id="diagram">
    <bpmndi:BPMNPlane id="plane" bpmnElement="complex-loop-process">
      <bpmndi:BPMNShape id="start_di" bpmnElement="start"><dc:Bounds x="152" y="272" width="36" height="36"/></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="task_preparar_di" bpmnElement="task_preparar"><dc:Bounds x="240" y="250" width="100" height="80"/></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="task_inspeccion_di" bpmnElement="task_inspeccion"><dc:Bounds x="400" y="250" width="100" height="80"/></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="gw_resultado_di" bpmnElement="gw_resultado" isMarkerVisible="true"><dc:Bounds x="565" y="265" width="50" height="50"/></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="task_documentar_di" bpmnElement="task_documentar"><dc:Bounds x="680" y="140" width="100" height="80"/></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="task_correcciones_di" bpmnElement="task_correcciones"><dc:Bounds x="680" y="390" width="100" height="80"/></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="task_rechazo_di" bpmnElement="task_rechazo"><dc:Bounds x="680" y="530" width="100" height="80"/></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="task_informe_rechazo_di" bpmnElement="task_informe_rechazo"><dc:Bounds x="840" y="530" width="100" height="80"/></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="task_entrega_di" bpmnElement="task_entrega"><dc:Bounds x="840" y="140" width="100" height="80"/></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="task_certificado_di" bpmnElement="task_certificado"><dc:Bounds x="1000" y="140" width="100" height="80"/></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="gw_merge_di" bpmnElement="gw_merge" isMarkerVisible="true"><dc:Bounds x="1165" y="265" width="50" height="50"/></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="task_registro_di" bpmnElement="task_registro"><dc:Bounds x="1280" y="250" width="100" height="80"/></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="end_di" bpmnElement="end"><dc:Bounds x="1452" y="272" width="36" height="36"/></bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="e_start" bpmnElement="flow_start_inspeccion"><di:waypoint x="188" y="290"/><di:waypoint x="240" y="290"/></bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="e_prep" bpmnElement="flow_preparar_inspeccion"><di:waypoint x="340" y="290"/><di:waypoint x="400" y="290"/></bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="e_insp_gw" bpmnElement="flow_inspeccion_gw"><di:waypoint x="500" y="290"/><di:waypoint x="565" y="290"/></bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="e_aprobado" bpmnElement="flow_gw_aprobado"><di:waypoint x="590" y="265"/><di:waypoint x="590" y="180"/><di:waypoint x="680" y="180"/></bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="e_obs" bpmnElement="flow_gw_obs_menores"><di:waypoint x="590" y="315"/><di:waypoint x="590" y="430"/><di:waypoint x="680" y="430"/></bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="e_rechazo" bpmnElement="flow_gw_rechazo"><di:waypoint x="590" y="315"/><di:waypoint x="590" y="570"/><di:waypoint x="680" y="570"/></bpmndi:BPMNEdge>
      <!-- LOOP back arrow -->
      <bpmndi:BPMNEdge id="e_loop" bpmnElement="flow_correc_inspeccion">
        <di:waypoint x="730" y="390"/><di:waypoint x="730" y="340"/><di:waypoint x="450" y="340"/><di:waypoint x="450" y="330"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="e_rec_inf" bpmnElement="flow_rechazo_informe"><di:waypoint x="780" y="570"/><di:waypoint x="840" y="570"/></bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="e_inf_merge" bpmnElement="flow_informe_merge"><di:waypoint x="940" y="570"/><di:waypoint x="1190" y="570"/><di:waypoint x="1190" y="315"/></bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="e_doc_entrega" bpmnElement="flow_documentar_entrega"><di:waypoint x="780" y="180"/><di:waypoint x="840" y="180"/></bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="e_entrega_cert" bpmnElement="flow_entrega_certificado"><di:waypoint x="940" y="180"/><di:waypoint x="1000" y="180"/></bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="e_cert_merge" bpmnElement="flow_cert_merge"><di:waypoint x="1100" y="180"/><di:waypoint x="1190" y="180"/><di:waypoint x="1190" y="265"/></bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="e_merge_reg" bpmnElement="flow_merge_registro"><di:waypoint x="1215" y="290"/><di:waypoint x="1280" y="290"/></bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="e_reg_end" bpmnElement="flow_registro_end"><di:waypoint x="1380" y="290"/><di:waypoint x="1452" y="290"/></bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>

</definitions>
