# ---------------------------------------------------------------------------
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ---------------------------------------------------------------------------

apiVersion: camel.apache.org/v1
kind: Kamelet
metadata:
  name: azure-cosmosdb-sink
  annotations:
    camel.apache.org/kamelet.support.level: "Stable"
    camel.apache.org/catalog.version: "4.10.2"
    camel.apache.org/kamelet.icon: "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAxOCAxOCI+PGRlZnM+PHJhZGlhbEdyYWRpZW50IGlkPSJhIiBjeD0iLTEwNS4wMDYiIGN5PSItMTAuNDA5IiByPSI1Ljk1NCIgZ3JhZGllbnRUcmFuc2Zvcm09Im1hdHJpeCgxLjAzNiAwIDAgMS4wMjcgMTE3LjczOSAxOS42NDQpIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSI+PHN0b3Agb2Zmc2V0PSIuMTgzIiBzdG9wLWNvbG9yPSIjNWVhMGVmIi8+PHN0b3Agb2Zmc2V0PSIxIiBzdG9wLWNvbG9yPSIjMDA3OGQ0Ii8+PC9yYWRpYWxHcmFkaWVudD48Y2xpcFBhdGggaWQ9ImIiPjxwYXRoIGQ9Ik0xNC45NjkgNy41M2E2LjEzNyA2LjEzNyAwIDExLTcuMzk1LTQuNTQzIDYuMTM3IDYuMTM3IDAgMDE3LjM5NSA0LjU0M3oiIGZpbGw9Im5vbmUiLz48L2NsaXBQYXRoPjwvZGVmcz48cGF0aCBkP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4LjQzNCA4LjQzNCAwIDAwMS4yNTEtLjEgMTcuODU1IDE3Ljg1NSAwIDAwNi4yMTktMi40QTE3LjgwOCAxNy44MDggMCAwMDE2LjI0IDguMDNjMS4yNDMtMS42NjEgMS41NzktMy4xNS45NTEtNC4xOTh6IiBmaWxsPSIjNTBlNmZmIi8+PC9zdmc+"
    camel.apache.org/provider: "Apache Software Foundation"
    camel.apache.org/kamelet.group: "Azure CosmosDB"
    camel.apache.org/kamelet.namespace: "Azure"
  labels:
    camel.apache.org/kamelet.type: "sink"
spec:
  definition:
    title: "Azure CosmosDB Sink"
    description: Send Data to an Azure CosmosDB instance
    required:
      - databaseName
      - containerName
      - databaseEndpoint
    type: object
    properties:
      databaseName:
        title: Database Name
        description: The Azure Cosmos database name.
        type: string
      containerName:
        title: Container Name
        description: The Azure Cosmos container name.
        type: string
      accountKey:
        title: Account Key
        description: The Azure Cosmos account Key.
        type: string
        format: password
        x-descriptors:
        - urn:camel:group:credentials
      databaseEndpoint:
        title: Database Endpoint
        description: Sets the Azure Cosmos database endpoint the component will connect to.
        type: string
      itemPartitionKey:
        title: Item Partition Key
        description: Represents a partition key value in the Azure Cosmos DB database service. A partition key identifies the partition where the item is stored in.
        type: string
      credentialType:
        title: Credential Type
        description: Determines the credential strategy to adopt.
        type: string
        enum: ["SHARED_ACCOUNT_KEY", "AZURE_IDENTITY"]
        default: "SHARED_ACCOUNT_KEY"
  types:
    in:
      mediaType: application/json
  dependencies:
    - "camel:azure-cosmosdb"
    - "camel:kamelet"
    - "camel:jackson"
    - "camel:core"
  template:
    from:
      uri: "kamelet:source"
      steps:
      - choice:
          precondition: true
          when:
            - simple: '${propertiesExist:!itemPartitionKey}'
              steps:
                - choice:   
                    when:
                      - simple: "${header[itemPartitionKey]}"
                        steps:
                          - setHeader:
                              name: CamelAzureCosmosDbItemPartitionKey
                              simple: "${header[itemPartitionKey]}"
                      - simple: "${header[ce-itemPartitionKey]}"
                        steps:
                          - setHeader:
                              name: CamelAzureCosmosDbItemPartitionKey
                              simple: "${header[ce-itemPartitionKey]}"
      - unmarshal:
          json: 
            library: Jackson
      - to:
          uri: "azure-cosmosdb:{{databaseName}}/{{containerName}}"
          parameters:
            accountKey: "{{?accountKey}}"
            databaseEndpoint: "{{databaseEndpoint}}"
            itemPartitionKey: "{{?itemPartitionKey}}"
            operation: createItem
            credentialType: "{{credentialType}}"