// AUTO-GENERATED — do not edit by hand
// Upstream: https://github.com/n8n-io/n8n/blob/master/packages/nodes-base/nodes/Google/Ads/GoogleAds.node.ts
// To implement: copy to ../impl/googleads.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 GoogleadsNode;

static META: NodeMetadata = NodeMetadata {
    name: "googleads",
    display_name: "Google Ads",
    version: &[1],
    group: &["transform"],
    description: "Use the Google Ads API",
    inputs: 1,
    outputs: 1,
    properties: &[
    PropertyDef { name: "resource", display_name: "Resource", kind: PropertyKind::Options, required: false, default: Some("campaign") },
    PropertyDef { name: "campaigsNotice", display_name: "Divide field names expressed with <i>micros</i> by 1,000,000 to get the actual value", kind: PropertyKind::Notice, required: false, default: Some("") }
],
    credentials: &[
    CredentialRef { name: "googleAdsOAuth2Api", required: true }
],
};

#[async_trait]
impl Node for GoogleadsNode {
    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://googleads.googleapis.com",
            credential: Some("googleAdsOAuth2Api"),
            auth_headers: &[],
            auth_query: &[],
            dispatch_param: "operation",
            operations: &[
                crate::engine::routing::OperationDescriptor {
                    value: "getAll", method: "POST", url_template: "{{",
                    body_template: "query:\n\t\t\t\t\t\t\t\t'={{ \"' +\n\t\t\t\t\t\t\t\t'select ' +\n\t\t\t\t\t\t\t\t'campaign.id, ' +\n\t\t\t\t\t\t\t\t'campaign.name, ' +\n\t\t\t\t\t\t\t\t'campaign_budget.amount_micros, ' +\n\t\t\t\t\t\t\t\t'campaign_budget.period,' +\n\t\t\t\t\t\t\t\t'campaign.status,' +\n\t\t\t\t\t\t\t\t'campaign.optimization_score,' +\n\t\t\t\t\t\t\t\t'campaign.advertising_channel_type,' +\n\t\t\t\t\t\t\t\t'campaign.advertising_channel_sub_type,' +\n\t\t\t\t\t\t\t\t'metrics.impressions,' +\n\t\t\t\t\t\t\t\t'metrics.interactions,' +\n\t\t\t\t\t\t\t\t'metrics.interaction_rate,' +\n\t\t\t\t\t\t\t\t'metrics.average_cost,' +\n\t\t\t\t\t\t\t\t'metrics.cost_micros,' +\n\t\t\t\t\t\t\t\t'metrics.conversions,' +\n\t\t\t\t\t\t\t\t'metrics.cost_per_conversion,' +\n\t\t\t\t\t\t\t\t'metrics.conversions_from_interactions_rate,' +\n\t\t\t\t\t\t\t\t'metrics.video_views,' +\n\t\t\t\t\t\t\t\t'metrics.average_cpm,' +\n\t\t\t\t\t\t\t\t'metrics.ctr ' +\n\t\t\t\t\t\t\t\t'from campaign ' +\n\t\t\t\t\t\t\t\t'where campaign.id > 0 ' + // create a dummy where clause so we can append more conditions\n\t\t\t\t\t\t\t\t'\" + ([\"allTime\", undefined, \"\"].includes($parameter.additionalOptions?.dateRange) ? \"\" : \" and segments.date DURING \" + $parameter.additionalOptions.dateRange) + \" ' +\n\t\t\t\t\t\t\t\t'\" + ([\"all\", undefined, \"\"].includes($parameter.additionalOptions?.campaignStatus) ? \"\" : \" and campaign.status = \\'\" + $parameter.additionalOptions.campaignStatus + \"\\'\") + \"' +\n\t\t\t\t\t\t\t\t'\" }}',", query: &[],
                },
                crate::engine::routing::OperationDescriptor {
                    value: "get", method: "POST", url_template: "{{",
                    body_template: "query:\n\t\t\t\t\t\t\t\t'={{ \"' +\n\t\t\t\t\t\t\t\t'select ' +\n\t\t\t\t\t\t\t\t'campaign.id, ' +\n\t\t\t\t\t\t\t\t'campaign.name, ' +\n\t\t\t\t\t\t\t\t'campaign_budget.amount_micros, ' +\n\t\t\t\t\t\t\t\t'campaign_budget.period,' +\n\t\t\t\t\t\t\t\t'campaign.status,' +\n\t\t\t\t\t\t\t\t'campaign.optimization_score,' +\n\t\t\t\t\t\t\t\t'campaign.advertising_channel_type,' +\n\t\t\t\t\t\t\t\t'campaign.advertising_channel_sub_type,' +\n\t\t\t\t\t\t\t\t'metrics.impressions,' +\n\t\t\t\t\t\t\t\t'metrics.interactions,' +\n\t\t\t\t\t\t\t\t'metrics.interaction_rate,' +\n\t\t\t\t\t\t\t\t'metrics.average_cost,' +\n\t\t\t\t\t\t\t\t'metrics.cost_micros,' +\n\t\t\t\t\t\t\t\t'metrics.conversions,' +\n\t\t\t\t\t\t\t\t'metrics.cost_per_conversion,' +\n\t\t\t\t\t\t\t\t'metrics.conversions_from_interactions_rate,' +\n\t\t\t\t\t\t\t\t'metrics.video_views,' +\n\t\t\t\t\t\t\t\t'metrics.average_cpm,' +\n\t\t\t\t\t\t\t\t'metrics.ctr ' +\n\t\t\t\t\t\t\t\t'from campaign ' +\n\t\t\t\t\t\t\t\t'where campaign.id = \" + $parameter[\"campaignId\"].toString().replace(/-/g, \"\")' +\n\t\t\t\t\t\t\t\t'}}',", query: &[],
                }
            ],
            sends: &[],
        };
    crate::engine::routing::execute_routing(&ROUTING, _ctx).await
    }
}

inventory::submit! { NodeFactory::new("googleads", || Arc::new(GoogleadsNode)) }
