// AUTO-GENERATED — do not edit by hand
// Upstream: https://github.com/n8n-io/n8n/blob/master/packages/nodes-base/nodes/Brevo/Brevo.node.ts
// To implement: copy to ../impl/sendinblue.rs, fill in execute(), add to ../impl/mod.rs
// r8r: rust port of n8n.io
// License: Unlicense

use async_trait::async_trait;
use crate::core::error::NodeError;
use crate::core::types::*;
use crate::engine::{ExecuteCtx, Node, NodeFactory};
use std::sync::Arc;

pub struct SendinblueNode;

static META: NodeMetadata = NodeMetadata {
    name: "sendinblue",
    display_name: "Brevo",
    version: &[1],
    group: &["transform"],
    description: "Consume Brevo API",
    inputs: 1,
    outputs: 1,
    properties: &[
    PropertyDef { name: "resource", display_name: "Resource", kind: PropertyKind::Options, required: false, default: Some("email") }
],
    credentials: &[
    CredentialRef { name: "sendInBlueApi", required: true }
],
};

#[async_trait]
impl Node for SendinblueNode {
    fn metadata(&self) -> &'static NodeMetadata { &META }

    async fn execute(&self, _ctx: &mut ExecuteCtx<'_>) -> Result<NodeOutput, NodeError> {
    static ROUTING: crate::engine::routing::RoutingDescriptor =
        crate::engine::routing::RoutingDescriptor {
            base_url: "https://api.brevo.com",
            credential: Some("sendInBlueApi"),
            auth_headers: &[("api-key", "{{apiKey}}")],
            auth_query: &[],
            dispatch_param: "operation",
            operations: &[
                crate::engine::routing::OperationDescriptor {
                    value: "sendTemplate", method: "POST", url_template: "/v3/smtp/email",
                    body_template: "", query: &[],
                },
                crate::engine::routing::OperationDescriptor {
                    value: "create", method: "POST", url_template: "/v3/contacts",
                    body_template: "", query: &[],
                },
                crate::engine::routing::OperationDescriptor {
                    value: "upsert", method: "POST", url_template: "/v3/contacts",
                    body_template: "", query: &[],
                },
                crate::engine::routing::OperationDescriptor {
                    value: "getAll", method: "GET", url_template: "/v3/contacts",
                    body_template: "", query: &[],
                },
                crate::engine::routing::OperationDescriptor {
                    value: "delete", method: "DELETE", url_template: "/v3/senders/{{$parameter.id}}",
                    body_template: "", query: &[],
                },
                crate::engine::routing::OperationDescriptor {
                    value: "update", method: "PUT", url_template: "/v3/contacts/attributes/{{$parameter.updateAttributeCategory}}/{{encodeURI($parameter.updateAttributeName)}}",
                    body_template: "", query: &[],
                }
            ],
            sends: &[
                crate::engine::routing::SendDescriptor {
                    param_name: "subject", send_type: "body", dest_key: "subject",
                    operations: &["send"], resources: &["email"],
                },
                crate::engine::routing::SendDescriptor {
                    param_name: "textContent", send_type: "body", dest_key: "textContent",
                    operations: &["send"], resources: &["email"],
                },
                crate::engine::routing::SendDescriptor {
                    param_name: "htmlContent", send_type: "body", dest_key: "htmlContent",
                    operations: &["send"], resources: &["email"],
                },
                crate::engine::routing::SendDescriptor {
                    param_name: "templateId", send_type: "body", dest_key: "templateId",
                    operations: &["sendTemplate"], resources: &["email"],
                },
                crate::engine::routing::SendDescriptor {
                    param_name: "email", send_type: "body", dest_key: "email",
                    operations: &["create"], resources: &["contact"],
                },
                crate::engine::routing::SendDescriptor {
                    param_name: "fieldValue", send_type: "body", dest_key: "=attributes.{{$parent.fieldName}}",
                    operations: &["create"], resources: &["contact"],
                },
                crate::engine::routing::SendDescriptor {
                    param_name: "limit", send_type: "query", dest_key: "limit",
                    operations: &["getAll"], resources: &["contact"],
                },
                crate::engine::routing::SendDescriptor {
                    param_name: "ASC", send_type: "query", dest_key: "sort",
                    operations: &["getAll"], resources: &["contact"],
                },
                crate::engine::routing::SendDescriptor {
                    param_name: "modifiedSince", send_type: "query", dest_key: "modifiedSince",
                    operations: &["getAll"], resources: &["contact"],
                },
                crate::engine::routing::SendDescriptor {
                    param_name: "name", send_type: "body", dest_key: "name",
                    operations: &["create"], resources: &["sender"],
                },
                crate::engine::routing::SendDescriptor {
                    param_name: "Text", send_type: "body", dest_key: "type",
                    operations: &["create"], resources: &["attribute"],
                },
                crate::engine::routing::SendDescriptor {
                    param_name: "attributeValue", send_type: "body", dest_key: "value",
                    operations: &["create"], resources: &["attribute"],
                },
                crate::engine::routing::SendDescriptor {
                    param_name: "attributeCategoryValue", send_type: "body", dest_key: "=enumeration[{{$index}}].value",
                    operations: &["create"], resources: &["attribute"],
                },
                crate::engine::routing::SendDescriptor {
                    param_name: "attributeCategoryLabel", send_type: "body", dest_key: "=enumeration[{{$index}}].label",
                    operations: &["create"], resources: &["attribute"],
                },
                crate::engine::routing::SendDescriptor {
                    param_name: "updateAttributeValue", send_type: "body", dest_key: "value",
                    operations: &["update"], resources: &["attribute"],
                }
            ],
        };
    crate::engine::routing::execute_routing(&ROUTING, _ctx).await
    }
}

inventory::submit! { NodeFactory::new("sendinblue", || Arc::new(SendinblueNode)) }
