
    `jC                        d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	m
Z
  ej                  de	        ej                  de       dd	d	d
	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 ddZddZy)u  SOAP envelope builder for Ross1000 Regione Liguria integration.

Wraps a plain Ross1000 XML payload in a SOAP 1.1 envelope compatible with:
    https://turismows.regione.liguria.it/ws/checkinV2?wsdl

The envelope structure follows SOAP 1.1 (SOAPAction header required by the
Liguria endpoint). The inner payload is the same XML generated by
movement_serializer.py, embedded as the body content.

Live transmission is NOT implemented — this module only builds the envelope.
A future transport client should:
1. Call build_soap_envelope(xml_payload)
2. POST the result to the WSDL endpoint with Content-Type: text/xml
3. Include SOAPAction header as required by the WSDL operation
    )annotations)BytesIO)ElementTree)ROSS1000_NSSOAP_ENV_NSWS_SECURITY_NSsoapenvturinviaMovimenti )	operationusernamepasswordc               h   t        j                  dt         d      }t        j                  |dt         d      }|r|rt	        |||       t        j                  |dt         d      }t        j                  |dt
         d|       }t        j                  |d      }| |_        t        |      S )u  Wrap a Ross1000 XML payload in a SOAP 1.1 envelope.

    Args:
        xml_payload: The plain Ross1000 XML string (from movement_serializer)
        operation:   WSDL operation name (default: "inviaMovimenti")
        username:    Optional ISTAT credential username (for WS-Security header)
        password:    Optional ISTAT credential password (for WS-Security header)

    Returns:
        UTF-8 SOAP envelope XML string ready for HTTP POST.

    Note:
        Credentials are included in the SOAP Header only when both username
        and password are non-empty. For now this is a placeholder — the
        actual authentication scheme must be confirmed against the WSDL.
    {z	}Envelopez}Header)r   r   z}Body}payload)r   Elementr   
SubElement_add_wssecurity_headerr   text_to_utf8_xml)	xml_payloadr   r   r   envelopeheaderbodyoperation_el
payload_els	            0/backend/istat/ross1000/soap/envelope_builder.pybuild_soap_enveloper    !   s    2 ""R}J#?@H ##H;-x.HIFHv8L !!(bV,DED))$"[MI;0OPL ''i@J!JO!!    c               D   t        j                  dt               t        j                  | dt         d      }t        j                  |dt         d      }t        j                  |dt         d      }||_        t        j                  |dt         d      }||_        y)zAdd a WS-Security UsernameToken to the SOAP header.

    This is a placeholder implementation. The actual WS-Security scheme
    (UsernameToken, digest, nonce, etc.) must be confirmed against the
    Regione Liguria WSDL before live transmission.
    wsser   z	}Securityz}UsernameTokenz	}Usernamez	}PasswordN)r   register_namespacer   r   r   )r   r   r   securitytokenuser_elpass_els          r   r   r   M   s     ""6>:%%f>2B*.MNH""8r.1A-QRE$$Ub0@
,KLGGL$$Ub0@
,KLGGLr!   c                    t        j                   |       }t               }|j                  |ddd       |j                         j	                  d      S )z@Serialize an ElementTree to a UTF-8 XML string with declaration.zUTF-8TF)encodingxml_declarationshort_empty_elements)r   r   writegetvaluedecode)roottreebufs      r   r   r   e   sE    ""4(D
)CJJsWdQVJW<<>  ))r!   N)
r   strr   r3   r   r3   r   r3   returnr3   )r   ElementTree.Elementr   r3   r   r3   r4   None)r0   r5   r4   r3   )__doc__
__future__r   ior   	xml.etreer   istat.ross1000.soap.schemasr   r   r   r$   r    r   r    r!   r   <module>r=      s     #  !    y+ 6   uk 2 &)")" )" 	)"
 )" 	)"X  	
 
0*r!   