Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq

Public Class cModalTransNCTS
    ' Haupt-Requestklasse für NCTS-Versandverfahren
    Public Class cMT_NCTS_DeclarationRequest
        ' Nachricht
        Public Property message_sender As String = "AVISO.APP"
        Public Property message_sender_code As String = "" '"ITTR0000000000153"
        Public Property message_recipient As String = "" '"NTA.BG"
        Public Property message_recipient_code As String '= "BG005804"
        Public Property message_identification As String = "BG015C"
        Public Property message_type As String = "BG015C"

        ' Zusätzliche Pflichtfelder lt. Fehlermeldung
        Public Property destination_country_id As String
        Public Property trans_method As String
        Public Property vehicle_id As String
        Public Property trailer_id As String

        ' TransitOperation
        Public Property lrn As String = "LRN-DEMO-12345"
        Public Property declaration_type As String = "T1"
        Public Property additional_declaration_type As String = "A"
        Public Property security_level As String = "3"
        Public Property reduced_dataset_indicator As String = "0"
        Public Property binding_itinerary As String = "0"

        ' Zollstellen
        Public Property departure_office As String
        Public Property destination_office As String
        Public Property transit_office_ref As String
        Public Property transit_arrival_est As String

        ' Inhaber des Verfahrens
        Public Property holder_eori As String
        Public Property holder_name As String
        Public Property holder_address As String
        Public Property holder_postcode As String
        Public Property holder_city As String
        Public Property holder_country As String
        Public Property holder_contact_name As String
        Public Property holder_contact_phone As String
        Public Property holder_contact_email As String

        ' Guarantee
        Public Property guarantee_type As String
        Public Property guarantee_sequence As String
        Public Property guarantee_reference_sequence As String
        Public Property guarantee_grn As String
        Public Property guarantee_access_code As String
        Public Property guarantee_amount As String
        Public Property guarantee_currency As String

        ' Consignment
        Public Property consignment_country_of_destination As String
        Public Property consignment_container_indicator As String
        Public Property consignment_inland_mode As String
        Public Property consignment_border_mode As String
        Public Property consignment_gross_mass As String

        ' Carrier
        Public Property carrier_identification_number As String

        ' Consignor
        Public Property consignor_name As String
        Public Property consignor_street As String
        Public Property consignor_postcode As String
        Public Property consignor_city As String
        Public Property consignor_country As String

        ' Consignee
        Public Property consignee_name As String
        Public Property consignee_street As String
        Public Property consignee_postcode As String
        Public Property consignee_city As String
        Public Property consignee_country As String

        ' Transportmittel Abgangsort
        Public Property departure_transport_sequence As String
        Public Property departure_transport_type_id As String
        Public Property departure_transport_number As String
        Public Property departure_transport_nationality As String

        ' Routenländer
        Public Property routing_countries As List(Of String)

        ' Grenzüberschreitendes Transportmittel
        Public Property border_transport_customs_office As String
        Public Property border_transport_type_id As String
        Public Property border_transport_number As String
        Public Property border_transport_nationality As String

        ' Beladungs- / Entladeort
        Public Property loading_country As String
        Public Property loading_location As String
        Public Property unloading_country As String
        Public Property unloading_location As String

        ' Steuer
        Public Property tax_total_sum As String
        Public Property tax_entries As List(Of cMT_NCTS_Tax)

        ' Hausversand / Warenpositionen
        Public Property house_consignments As List(Of cMT_NCTS_HouseConsignment)
    End Class

    Public Class cMT_NCTS_Tax
        Public Property sequence_number As String
        Public Property tax_type As String
        Public Property tax_sum As String
    End Class

    Public Class cMT_NCTS_HouseConsignment
        Public Property sequence_number As String
        Public Property gross_mass As String
        Public Property items As List(Of cMT_NCTS_Item)
    End Class

    Public Class cMT_NCTS_Item
        Public Property goods_item_number As String
        Public Property declaration_goods_item_number As String
        Public Property description As String
        Public Property hs_code As String
        Public Property gross_mass As String
        Public Property net_mass As String
        Public Property packages As List(Of cMT_NCTS_Package)
        Public Property supporting_documents As List(Of cMT_NCTS_Document)
        Public Property transport_documents As List(Of cMT_NCTS_Document)
    End Class

    Public Class cMT_NCTS_Package
        Public Property sequence_number As String
        Public Property package_type As String
        Public Property number_of_packages As String
        Public Property marks As String
    End Class

    Public Class cMT_NCTS_Document
        Public Property sequence_number As String
        Public Property doc_type As String
        Public Property reference As String
    End Class


    ' Response-Klasse für NCTS-Antworten
    Public Class cMT_NCTS_DeclarationResponse
        Public Property status As String
        Public Property message As String
        Public Property declaration_id As String
        Public Property mrn As String
        Public Property lrn As String
        Public Property created_at As String
    End Class


    Public Class cMT_NCTS_ApiClient
        Private http As New Chilkat.Http()
        Private authToken As String

        Public Sub New()
            VERAG_PROG_ALLGEMEIN.cChilkat_Helper.UnlockCilkat()

            http.SetRequestHeader("Content-Type", "application/json")
        End Sub



        ' SEND NCTS Declaration using object and convert to wrapped XML JSON
        Public Function SendNCTSDeclaration(declaration As cMT_NCTS_DeclarationRequest) As cMT_NCTS_DeclarationResponse
            Dim result As New cMT_NCTS_DeclarationResponse()

            If Not cModalTransBase.Authenticate(authToken) Then
                result.status = "Error"
                result.message = "❌ Authentifizierung fehlgeschlagen."
                Return result
            End If

            ' XML manuell generieren aus dem Objekt

            Dim xml As New Chilkat.Xml()
            xml.Tag = "ie:BG015C"
            xml.AddAttribute("xmlns:ie", "http://ncts.dgtaxud.ec")

            xml.UpdateChildContent("messageSender", declaration.message_sender)
            xml.UpdateChildContent("messageSenderCode", declaration.message_sender_code)
            xml.UpdateChildContent("messageRecipient", declaration.message_recipient)
            xml.UpdateChildContent("messageRecipientCode", declaration.message_recipient_code)
            xml.UpdateChildContent("preparationDateAndTime", DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss"))
            xml.UpdateChildContent("messageIdentification", declaration.message_identification)
            xml.UpdateChildContent("messageType", declaration.message_type)

            Dim transitOp = xml.NewChild("TransitOperation", "")
            transitOp.UpdateChildContent("LRN", declaration.lrn)
            transitOp.UpdateChildContent("declarationType", declaration.declaration_type)
            transitOp.UpdateChildContent("additionalDeclarationType", declaration.additional_declaration_type)
            transitOp.UpdateChildContent("security", declaration.security_level)
            transitOp.UpdateChildContent("reducedDatasetIndicator", declaration.reduced_dataset_indicator)
            transitOp.UpdateChildContent("bindingItinerary", declaration.binding_itinerary)
            MsgBox(declaration.vehicle_id)
            xml.UpdateChildContent("vehicleId", declaration.vehicle_id)
            xml.UpdateChildContent("trailerId", declaration.trailer_id)
            xml.UpdateChildContent("CustomsOfficeOfDeparture/referenceNumber", declaration.departure_office)
            xml.UpdateChildContent("CustomsOfficeOfDestinationDeclared/referenceNumber", declaration.destination_office)

            If Not String.IsNullOrWhiteSpace(declaration.transit_office_ref) Then
                Dim transitOffice = xml.NewChild("CustomsOfficeOfTransitDeclared", "")
                transitOffice.UpdateChildContent("sequenceNumber", "1")
                transitOffice.UpdateChildContent("referenceNumber", declaration.transit_office_ref)
                transitOffice.UpdateChildContent("arrivalDateAndTimeEstimated", declaration.transit_arrival_est)
            End If

            Dim holder = xml.NewChild("HolderOfTheTransitProcedure", "")
            holder.UpdateChildContent("identificationNumber", declaration.holder_eori)
            holder.UpdateChildContent("name", declaration.holder_name)
            Dim holderAddr = holder.NewChild("Address", "")
            holderAddr.UpdateChildContent("streetAndNumber", declaration.holder_address)
            holderAddr.UpdateChildContent("postcode", declaration.holder_postcode)
            holderAddr.UpdateChildContent("city", declaration.holder_city)
            holderAddr.UpdateChildContent("country", declaration.holder_country)

            If Not String.IsNullOrWhiteSpace(declaration.holder_contact_name) Then
                Dim contact = holder.NewChild("ContactPerson", "")
                contact.UpdateChildContent("name", declaration.holder_contact_name)
                contact.UpdateChildContent("phoneNumber", declaration.holder_contact_phone)
                contact.UpdateChildContent("eMailAddress", declaration.holder_contact_email)
            End If

            Dim guarantee = xml.NewChild("Guarantee", "")
            guarantee.UpdateChildContent("sequenceNumber", declaration.guarantee_sequence)
            guarantee.UpdateChildContent("guaranteeType", declaration.guarantee_type)
            Dim gr = guarantee.NewChild("GuaranteeReference", "")
            gr.UpdateChildContent("sequenceNumber", declaration.guarantee_reference_sequence)
            gr.UpdateChildContent("GRN", declaration.guarantee_grn)
            gr.UpdateChildContent("accessCode", declaration.guarantee_access_code)
            gr.UpdateChildContent("amountToBeCovered", declaration.guarantee_amount)
            gr.UpdateChildContent("currency", declaration.guarantee_currency)

            Dim cons = xml.NewChild("Consignment", "")
            cons.UpdateChildContent("countryOfDestination", declaration.consignment_country_of_destination)
            cons.UpdateChildContent("containerIndicator", declaration.consignment_container_indicator)
            cons.UpdateChildContent("inlandModeOfTransport", declaration.consignment_inland_mode)
            cons.UpdateChildContent("modeOfTransportAtTheBorder", declaration.consignment_border_mode)
            cons.UpdateChildContent("grossMass", declaration.consignment_gross_mass)

            Dim carrier = cons.NewChild("Carrier", "")
            carrier.UpdateChildContent("identificationNumber", declaration.carrier_identification_number)

            Dim consignor = cons.NewChild("Consignor", "")
            consignor.UpdateChildContent("name", declaration.consignor_name)
            Dim consignorAddr = consignor.NewChild("Address", "")
            consignorAddr.UpdateChildContent("streetAndNumber", declaration.consignor_street)
            consignorAddr.UpdateChildContent("postcode", declaration.consignor_postcode)
            consignorAddr.UpdateChildContent("city", declaration.consignor_city)
            consignorAddr.UpdateChildContent("country", declaration.consignor_country)

            Dim consignee = cons.NewChild("Consignee", "")
            consignee.UpdateChildContent("name", declaration.consignee_name)
            Dim consigneeAddr = consignee.NewChild("Address", "")
            consigneeAddr.UpdateChildContent("streetAndNumber", declaration.consignee_street)
            consigneeAddr.UpdateChildContent("postcode", declaration.consignee_postcode)
            consigneeAddr.UpdateChildContent("city", declaration.consignee_city)
            consigneeAddr.UpdateChildContent("country", declaration.consignee_country)

            Dim dtm = cons.NewChild("DepartureTransportMeans", "")
            dtm.UpdateChildContent("sequenceNumber", declaration.departure_transport_sequence)
            dtm.UpdateChildContent("typeOfIdentification", declaration.departure_transport_type_id)
            dtm.UpdateChildContent("identificationNumber", declaration.departure_transport_number)
            dtm.UpdateChildContent("nationality", declaration.departure_transport_nationality)

            If declaration.routing_countries IsNot Nothing Then
                For i = 0 To declaration.routing_countries.Count - 1
                    Dim rc = cons.NewChild("CountryOfRoutingOfConsignment", "")
                    rc.UpdateChildContent("sequenceNumber", (i + 1).ToString())
                    rc.UpdateChildContent("country", declaration.routing_countries(i))
                Next
            End If

            Dim activeBorder = cons.NewChild("ActiveBorderTransportMeans", "")
            activeBorder.UpdateChildContent("sequenceNumber", "1")
            activeBorder.UpdateChildContent("customsOfficeAtBorderReferenceNumber", declaration.border_transport_customs_office)
            activeBorder.UpdateChildContent("typeOfIdentification", declaration.border_transport_type_id)
            activeBorder.UpdateChildContent("identificationNumber", declaration.border_transport_number)
            activeBorder.UpdateChildContent("nationality", declaration.border_transport_nationality)

            Dim placeLoading = cons.NewChild("PlaceOfLoading", "")
            placeLoading.UpdateChildContent("country", declaration.loading_country)
            placeLoading.UpdateChildContent("location", declaration.loading_location)

            Dim placeUnloading = cons.NewChild("PlaceOfUnloading", "")
            placeUnloading.UpdateChildContent("country", declaration.unloading_country)
            placeUnloading.UpdateChildContent("location", declaration.unloading_location)

            Dim taxCalc = cons.NewChild("TaxCalculation", "")
            taxCalc.UpdateChildContent("totalSum", declaration.tax_total_sum)
            If declaration.tax_entries IsNot Nothing Then
                For Each tax In declaration.tax_entries
                    Dim taxNode = taxCalc.NewChild("Tax", "")
                    taxNode.UpdateChildContent("sequenceNumber", tax.sequence_number)
                    taxNode.UpdateChildContent("taxType", tax.tax_type)
                    taxNode.UpdateChildContent("taxSum", tax.tax_sum)
                Next
            End If

            If declaration.house_consignments IsNot Nothing Then
                For Each house In declaration.house_consignments
                    Dim hc = cons.NewChild("HouseConsignment", "")
                    hc.UpdateChildContent("sequenceNumber", house.sequence_number)
                    hc.UpdateChildContent("grossMass", house.gross_mass)
                    For Each item In house.items
                        Dim ci = hc.NewChild("ConsignmentItem", "")
                        ci.UpdateChildContent("goodsItemNumber", item.goods_item_number)
                        ci.UpdateChildContent("declarationGoodsItemNumber", item.declaration_goods_item_number)
                        Dim com = ci.NewChild("Commodity", "")
                        com.UpdateChildContent("descriptionOfGoods", item.description)
                        Dim cc = com.NewChild("CommodityCode", "")
                        cc.UpdateChildContent("harmonizedSystemSubHeadingCode", item.hs_code)
                        Dim gm = com.NewChild("GoodsMeasure", "")
                        gm.UpdateChildContent("grossMass", item.gross_mass)
                        gm.UpdateChildContent("netMass", item.net_mass)
                        For Each pkg In item.packages
                            Dim pk = ci.NewChild("Packaging", "")
                            pk.UpdateChildContent("sequenceNumber", pkg.sequence_number)
                            pk.UpdateChildContent("typeOfPackages", pkg.package_type)
                            pk.UpdateChildContent("numberOfPackages", pkg.number_of_packages)
                            pk.UpdateChildContent("shippingMarks", pkg.marks)
                        Next
                        For Each doc In item.supporting_documents
                            Dim d = ci.NewChild("SupportingDocument", "")
                            d.UpdateChildContent("sequenceNumber", doc.sequence_number)
                            d.UpdateChildContent("type", doc.doc_type)
                            d.UpdateChildContent("referenceNumber", doc.reference)
                        Next
                        For Each doc In item.transport_documents
                            Dim d = ci.NewChild("TransportDocument", "")
                            d.UpdateChildContent("sequenceNumber", doc.sequence_number)
                            d.UpdateChildContent("type", doc.doc_type)
                            d.UpdateChildContent("referenceNumber", doc.reference)
                        Next
                    Next
                Next
            End If

            Dim xmlContent As String = xml.GetXml()
            Dim jsonPayload As String = JsonConvert.SerializeObject(New With {
            .manifesto = New With {
                .ncts_data = xmlContent
            }
        })



            Dim url As String = cModalTransBase.baseUrl & "/api/v1/ncts_declarations"
            http.SetRequestHeader("Authorization", "Bearer " & authToken)
            http.SetRequestHeader("Content-Type", "application/json")

            Dim resp As Chilkat.HttpResponse = http.PostJson(url, jsonPayload)

            If resp Is Nothing Then
                result.status = "Error"
                result.message = "❌ Keine Verbindung: " & http.LastErrorText
                Return result
            End If

            Select Case resp.StatusCode
                Case 201, 200
                    result.status = "Success"
                    result.message = "✅ NCTS-Anmeldung erfolgreich übermittelt."
                    Dim json = JsonConvert.DeserializeObject(Of JObject)(resp.BodyStr)
                    result.lrn = json.SelectToken("lrn")?.ToString()
                    result.mrn = json.SelectToken("mrn")?.ToString()
                    result.declaration_id = json.SelectToken("declaration_id")?.ToString()

                Case 400, 422
                    result.status = "Validation Error"
                    result.message = $"⚠️ Fehlerhafte Eingabe ({resp.StatusCode}): {resp.BodyStr}"

                Case 401
                    result.status = "Unauthorized"
                    result.message = "❌ Zugriff verweigert – bitte Authentifizierung prüfen."

                Case Else
                    result.status = "Error"
                    result.message = $"❌ Unerwarteter Fehler ({resp.StatusCode}): {resp.BodyStr}"
            End Select

            Console.WriteLine("Status: " & result.status)
            Console.WriteLine("Nachricht: " & result.message)
            Return result
        End Function

        ' GET Declaration by ID / MRN / LRN / Ref 
        Public Function GetNCTSDeclaration(identifier As String) As cMT_NCTS_DeclarationRequest
            If Not cModalTransBase.Authenticate(authToken) Then
                Console.WriteLine("❌ Authentifizierung fehlgeschlagen.")
                Return Nothing
            End If
            http.SetRequestHeader("Authorization", "Bearer " & authToken)

            Dim url = $"{cModalTransBase.baseUrl}/api/v1/ncts_declarations/{identifier}"
            Dim resp = http.QuickGetObj(url)

            If resp Is Nothing Then
                Console.WriteLine("❌ Keine Verbindung: " & http.LastErrorText)
                Return Nothing
            End If

            Select Case resp.StatusCode
                Case 200
                    Try
                        Dim json = JsonConvert.DeserializeObject(Of JObject)(resp.BodyStr)
                        Dim data = json.SelectToken("declaration_data")
                        Console.WriteLine("✅ NCTS-Anmeldung erfolgreich abgerufen.")
                        Return data.ToObject(Of cMT_NCTS_DeclarationRequest)()
                    Catch ex As Exception
                        Console.WriteLine("❌ Fehler beim Parsen: " & ex.Message)
                    End Try
                Case 401
                    Console.WriteLine("❌ Zugriff verweigert (401)")
                Case 404
                    Console.WriteLine("⚠️ Anmeldung nicht gefunden (404)")
                Case Else
                    Console.WriteLine($"❌ Fehler ({resp.StatusCode}): {resp.BodyStr}")
            End Select

            Return Nothing
        End Function

        ' LIST all declarations
        Public Function GetNCTSDeclarations(Optional page As Integer = 1) As List(Of cMT_NCTS_DeclarationResponse)
            Dim list As New List(Of cMT_NCTS_DeclarationResponse)()

            If Not cModalTransBase.Authenticate(authToken) Then
                Console.WriteLine("❌ Authentifizierung fehlgeschlagen.")
                Return list
            End If
            http.SetRequestHeader("Authorization", "Bearer " & authToken)

            Dim url = $"{cModalTransBase.baseUrl}/api/v1/ncts_declarations?page={page}"
            Dim resp = http.QuickGetObj(url)

            If resp Is Nothing Then
                Console.WriteLine("❌ Keine Verbindung: " & http.LastErrorText)
                Return list
            End If

            Select Case resp.StatusCode
                Case 200
                    Try
                        Dim json = JsonConvert.DeserializeObject(Of JObject)(resp.BodyStr)
                        Dim data = json.SelectToken("data")
                        If data IsNot Nothing Then
                            list = data.ToObject(Of List(Of cMT_NCTS_DeclarationResponse))()
                            Console.WriteLine("✅ Liste der NCTS-Anmeldungen erfolgreich abgerufen.")
                        End If
                    Catch ex As Exception
                        Console.WriteLine("❌ Fehler beim Parsen der Liste: " & ex.Message)
                    End Try
                Case 401
                    Console.WriteLine("❌ Zugriff verweigert (401)")
                Case Else
                    Console.WriteLine($"❌ Fehler ({resp.StatusCode}): {resp.BodyStr}")
            End Select

            Return list
        End Function
    End Class



    Public Shared Function ceateAndSend_NCTS_FromSENDUNG(LRN As String, SND As cSendungen) As Boolean
        If SND.tblSnd_AvisoID <= 0 Then Return False
        Dim AVISO As cAviso = (New cAvisoDAL).LesenAviso(SND.tblSnd_AvisoID, "")
        Return ceateAndSend_NCTS_FromSENDUNG(LRN, SND, AVISO)
    End Function


    Public Shared Function ceateAndSend_NCTS_FromSENDUNG(LRN As String, SND As cSendungen, AVISO As cAviso) As Boolean
        ' Objekt mit allen notwendigen Pflichtfeldern befüllen
        Dim MT_NCTS As New cMT_NCTS_DeclarationRequest With {
                     .lrn = LRN,
                    .departure_office = "GB000060",
                    .destination_office = "",
                    .transit_office_ref = "",
                            .trans_method = "3",
                    .transit_arrival_est = "2024-06-14T00:00:00",
                    .holder_eori = "DE828088257899835",
                    .holder_name = "VERAG SPEDITION GMBH",
                    .holder_address = "Hartham 6",
                    .holder_postcode = "94152",
                    .holder_city = "NEUHAUS AM INN",
                    .holder_country = "DE",
                    .holder_contact_name = VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME,
                    .holder_contact_phone = "0043771127770",
                    .holder_contact_email = "customs@verag-unisped.uk",
                    .departure_transport_sequence = "1",
                    .departure_transport_nationality = AVISO.LKW_Nationalitaet,
                    .border_transport_number = AVISO.LKW_Nr,
                    .border_transport_nationality = AVISO.LKW_Nationalitaet
                }



        Select Case If(AVISO.Transportmittelart, "")
            Case "CONTAINER"
                '    MT_NCTS.border_transport_type = "1" ' z. B. Ocean
                '  MT_NCTS.arrival_transport_type = "10"
                ' MT_NCTS.border_transport_type_id = "1" ' z. B. Straße '?6 RORO??
                '   MT_NCTS.departure_transport_type_id = "3" ' z. B. Straße '?6 RORO??
                MT_NCTS.consignment_inland_mode = "3"
                MT_NCTS.consignment_border_mode = "3"
                MT_NCTS.consignment_container_indicator = "1"

            Case Else
                MT_NCTS.consignment_container_indicator = "0"
                MT_NCTS.border_transport_type_id = "30"
                MT_NCTS.departure_transport_type_id = "30"
                MT_NCTS.consignment_inland_mode = "3"
                MT_NCTS.consignment_border_mode = "3"
                '          MT_NCTS.border_transport_type = "6" ' z. B. 3 Straße '?6 RORO??
                ' MT_NCTS.arrival_transport_type = "30"

        End Select

        MT_NCTS.departure_transport_number = AVISO.LKW_Nr
        MT_NCTS.vehicle_id = AVISO.LKW_Nr
        MT_NCTS.trailer_id = AVISO.LKW_Nr ' AVISO.LKW_Nationalitaet



        If SND.tblSnd_EmpfaengerKdNr > 0 Then
            Dim AD As New cAdressen(SND.tblSnd_EmpfaengerKdNr)
            Dim KD As New cKunde(SND.tblSnd_EmpfaengerKdNr)
            Dim CN As New VERAG_PROG_ALLGEMEIN.cRelayHub.cRelayHubAddress
            '  MT_NCTS.consignee_eori = If(KD.EORITIN, "")
            MT_NCTS.consignee_name = AD.Name_1
            MT_NCTS.consignee_street = AD.Straße
            MT_NCTS.consignee_postcode = AD.PLZ
            MT_NCTS.consignee_city = AD.Ort
            MT_NCTS.consignee_country = cProgramFunctions.getISO2Land(AD.LandKz)
            MT_NCTS.destination_country_id = MT_NCTS.consignee_country
            MT_NCTS.consignment_country_of_destination = MT_NCTS.consignee_country
            MT_NCTS.unloading_country = MT_NCTS.consignee_country
            MT_NCTS.unloading_location = MT_NCTS.consignee_country
        End If

        If SND.tblSnd_AbsenderKdNr > 0 Then
            Dim AD As New cAdressen(SND.tblSnd_AbsenderKdNr)
            Dim KD As New cKunde(SND.tblSnd_AbsenderKdNr)
            Dim CZ As New VERAG_PROG_ALLGEMEIN.cRelayHub.cRelayHubAddress

            '     MT_NCTS.consignor_city= If(KD.EORITIN, "")
            MT_NCTS.consignor_name = AD.Name_1
            MT_NCTS.consignor_street = AD.Straße
            MT_NCTS.consignor_postcode = AD.PLZ
            MT_NCTS.consignor_city = AD.Ort
            MT_NCTS.consignor_country = cProgramFunctions.getISO2Land(AD.LandKz)
            ' MT_NCTS.export_country = MT_NCTS.consignor_country_id
            MT_NCTS.loading_country = MT_NCTS.consignor_country
            MT_NCTS.loading_location = MT_NCTS.consignor_country
        End If



        ' API-Client erstellen (mit Benutzer & Passwort in der Klasse eingebaut)
        Dim client As New cMT_NCTS_ApiClient()

        ' Senden und Ergebnis anzeigen
        Dim response As cMT_NCTS_DeclarationResponse = client.SendNCTSDeclaration(MT_NCTS)

        If response IsNot Nothing Then
            Console.WriteLine("Status: " & response.status)
            Console.WriteLine("Message: " & response.message)
            Console.WriteLine("Declaration ID: " & response.declaration_id)
            Console.WriteLine("LRN: " & response.lrn)
            Console.WriteLine("MRN: " & response.mrn)
        Else
            Console.WriteLine("❌ Keine Antwort erhalten.")
        End If

        Return False
    End Function


    Public Shared Sub TestNCTSSend()
        Dim client As New cMT_NCTS_ApiClient()
        Dim declaration As New cMT_NCTS_DeclarationRequest With {
        .lrn = "LRN-DEMO-" & DateTime.Now.ToString("yyyyMMddHHmmss"),
        .departure_office = "BG005804",
        .destination_office = "TR343100",
        .transit_office_ref = "TR220200",
        .destination_country_id = "GB",
        .trans_method = "3",
        .transit_arrival_est = "2024-06-14T00:00:00",
        .holder_eori = "DE828088257899835",
        .holder_name = "VERAG SPEDITION AGMBH",
        .holder_address = "Hartham 6",
        .holder_postcode = "94152",
        .holder_city = "NEUHAUS AM INN",
        .holder_country = "DE",
        .holder_contact_name = "Luxbauer",
        .holder_contact_phone = "0771127770",
        .holder_contact_email = "customs@verag-unisped.uk",
        .guarantee_type = "0",
        .guarantee_sequence = "1",
        .guarantee_reference_sequence = "1",
        .guarantee_grn = "DE123456789",
        .guarantee_access_code = "XXXX",
        .guarantee_amount = "1",
        .guarantee_currency = "EUR",
        .consignment_country_of_destination = "TR",
        .consignment_container_indicator = "0",
        .consignment_inland_mode = "3",
        .consignment_border_mode = "3",
        .consignment_gross_mass = "10.000",
        .carrier_identification_number = "ateos13456",
        .consignor_name = "test",
        .consignor_street = "test",
        .consignor_postcode = "01111",
        .consignor_city = "test",
        .consignor_country = "at",
        .consignee_name = "test",
        .consignee_street = "test",
        .consignee_postcode = "34850",
        .consignee_city = "ISTANBUL",
        .consignee_country = "TR",
        .departure_transport_sequence = "1",
        .departure_transport_type_id = "30",
        .departure_transport_number = "123456789",
        .departure_transport_nationality = "TR",
        .routing_countries = New List(Of String) From {"RS", "BG", "TR"},
        .border_transport_customs_office = "tr132456",
        .border_transport_type_id = "30",
        .border_transport_number = "139786546",
        .border_transport_nationality = "TR",
        .loading_country = "BG",
        .loading_location = "BG",
        .unloading_country = "TR",
        .unloading_location = "TR",
         .vehicle_id = "16BCB723",
        .trailer_id = "16APM317",
        .tax_total_sum = "3",
        .tax_entries = New List(Of cMT_NCTS_Tax) From {
            New cMT_NCTS_Tax With {.sequence_number = "1", .tax_type = "A00", .tax_sum = "1"},
            New cMT_NCTS_Tax With {.sequence_number = "2", .tax_type = "B00", .tax_sum = "2"}
        },
        .house_consignments = New List(Of cMT_NCTS_HouseConsignment) From {
            New cMT_NCTS_HouseConsignment With {
                .sequence_number = "1",
                .gross_mass = "11777.000",
                .items = New List(Of cMT_NCTS_Item) From {
                    New cMT_NCTS_Item With {
                        .goods_item_number = "1",
                        .declaration_goods_item_number = "1",
                        .description = "Textilprodukte",
                        .hs_code = "380991",
                        .gross_mass = "1.000",
                        .net_mass = "1.000",
                        .packages = New List(Of cMT_NCTS_Package) From {
                            New cMT_NCTS_Package With {
                                .sequence_number = "1",
                                .package_type = "PK",
                                .number_of_packages = "1",
                                .marks = "NO MARKS"
                            }
                        },
                        .supporting_documents = New List(Of cMT_NCTS_Document) From {
                            New cMT_NCTS_Document With {.sequence_number = "1", .doc_type = "N380", .reference = "A1111"}
                        },
                        .transport_documents = New List(Of cMT_NCTS_Document) From {
                            New cMT_NCTS_Document With {.sequence_number = "1", .doc_type = "N730", .reference = "CMR"}
                        }
                    }
                }
            }
        }
    }
        Dim response = client.SendNCTSDeclaration(declaration)
        If response IsNot Nothing Then
            Console.WriteLine("Status: " & response.status)
            Console.WriteLine("Message: " & response.message)
            Console.WriteLine("Declaration ID: " & response.declaration_id)
            Console.WriteLine("LRN: " & response.lrn)
            Console.WriteLine("MRN: " & response.mrn)
        Else
            Console.WriteLine("❌ Keine Antwort erhalten.")
        End If
    End Sub
End Class