<?php
namespace Zaplane\Integrations;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use Zaplane\Traits\ActionResponseTrait;
use Zaplane\Framework\Classes\IntegrationBase;
use Zaplane\Framework\Models\Post;
use Zaplane\Framework\Models\User;
use Zaplane\Framework\Models\Comment;
use Zaplane\Integrations\Wordpress\PostActionsTrait;
use Zaplane\Integrations\Wordpress\TaxonomyActionsTrait;
use Zaplane\Integrations\Wordpress\UserActionsTrait;
use Zaplane\Integrations\Wordpress\RoleActionsTrait;
use Zaplane\Integrations\Wordpress\OptionActionsTrait;
use Zaplane\Integrations\Wordpress\MediaActionsTrait;
use Zaplane\Integrations\Wordpress\CommentActionsTrait;
use Zaplane\Integrations\Wordpress\QueryTrait;
use Zaplane\Integrations\Wordpress\Helper;

// phpcs:ignore WordPress.WP.CapitalPDangit.MisspelledClassName -- Kept for backwards compatibility.
class Wordpress extends IntegrationBase {

	use ActionResponseTrait;
	use PostActionsTrait;
	use TaxonomyActionsTrait;
	use UserActionsTrait;
	use RoleActionsTrait;
	use OptionActionsTrait;
	use MediaActionsTrait;
	use CommentActionsTrait;
	use QueryTrait;
	use Helper;

	public static function get_slug(): string {
		return 'wordpress';
	}

	public static function get_name(): string {
		return 'WordPress';
	}

	public static function get_icon(): string {
		return 'wordpress.svg';
	}

	/** Trigger events the recipe tester can self-seed with real WordPress data. */
	public static function get_seedable_triggers(): array {
		return [
			'publish_post',
			'save_post',
			'post_updated',
			'user_register',
			'profile_update',
			'comment_post',
			'create_term',
		];
	}

	/** Create real WordPress data and return the hook arguments for a trigger. */
	public static function seed_trigger_args( string $event ): ?array {
		switch ( $event ) {
			case 'publish_post':
			case 'save_post':
			case 'post_updated':
				$post_id = wp_insert_post(
					[
						'post_title'   => 'Zaplane Recipe Post',
						'post_content' => 'Created by a recipe seeder.',
						'post_status'  => 'publish',
						'post_type'    => 'post',
					]
				);
				if ( ! $post_id || is_wp_error( $post_id ) ) {
					return null;
				}
				$post = get_post( $post_id );
				if ( 'save_post' === $event ) {
					return [ $post_id, $post, false ];          // save_post( $id, $post, $update )
				}
				if ( 'post_updated' === $event ) {
					return [ $post_id, $post, $post ];          // post_updated( $id, $after, $before )
				}
				return [ $post_id, $post ];                     // publish_post( $id, $post )

			case 'user_register':
			case 'profile_update':
				$suffix  = substr( md5( uniqid( 'u', true ) ), 0, 8 );
				$user_id = wp_insert_user(
					[
						'user_login' => "recipe_{$suffix}",
						'user_email' => "recipe_{$suffix}@example.test",
						'user_pass'  => wp_generate_password( 16 ),
					]
				);
				if ( is_wp_error( $user_id ) ) {
					return null;
				}
				if ( 'profile_update' === $event ) {
					return [ $user_id, new \WP_User( $user_id ) ]; // profile_update( $id, $old_user_data )
				}
				return [ $user_id ];                               // user_register( $id )

			case 'comment_post':
				$post_id    = wp_insert_post( [
					'post_title' => 'Recipe Post',
					'post_status' => 'publish',
					'post_type' => 'post'
				] );
				$comment_id = wp_insert_comment(
					[
						'comment_post_ID'      => $post_id,
						'comment_author'       => 'Recipe',
						'comment_author_email' => 'recipe@example.test',
						'comment_content'      => 'Seeded comment.',
						'comment_approved'     => 1,
					]
				);
				return $comment_id ? [ $comment_id, 1 ] : null;    // comment_post( $comment_id, $approved )

			case 'create_term':
				$slug = 'recipe-' . substr( md5( uniqid( 't', true ) ), 0, 6 );
				$term = wp_insert_term( 'Recipe ' . $slug, 'category', [ 'slug' => $slug ] );
				if ( is_wp_error( $term ) ) {
					return null;
				}
				return [ $term['term_id'], $term['term_taxonomy_id'], 'category' ]; // create_term( $term_id, $tt_id, $taxonomy )
		}//end switch

		return null;
	}

	public static function get_output_ports(): array {
		return [ 'main', 'error' ];
	}

	public static function get_triggers(): array {
		return [

			'publish_post'           => [
				'label' => 'Post Published',
				'hook' => 'publish_post'
			],
			'post_updated'           => [
				'label' => 'Post Updated',
				'hook' => 'post_updated'
			],
			'transition_post_status' => [
				'label' => 'Post Status Updated',
				'hook' => 'transition_post_status'
			],
			'wp_insert_post'         => [
				'label' => 'Post Inserted',
				'hook' => 'wp_insert_post'
			],
			'wp_after_insert_post'   => [
				'label' => 'WP After Insert Post',
				'hook' => 'wp_after_insert_post'
			],
			'wp_trash_post'          => [
				'label' => 'Post Trashed',
				'hook' => 'wp_trash_post'
			],
			'untrashed_post'         => [
				'label' => 'Post Untrashed',
				'hook' => 'untrashed_post'
			],
			'delete_post'            => [
				'label' => 'Delete Post',
				'hook' => 'delete_post'
			],
			'deleted_post'           => [
				'label' => 'Before Deleted Post',
				'hook' => 'before_delete_post'
			],
			'save_post'              => [
				'label' => 'Save Post',
				'hook' => 'save_post'
			],
			'post_revision'          => [
				'label' => 'Post Revision Saved',
				'hook'  => '_wp_put_post_revision',
			],
			'wp_authenticate'        => [
				'label' => 'User Authenticate Attempt',
				'hook'  => 'wp_authenticate',
			],
			'add_attachment'              => [
				'label' => 'Add Attachment',
				'hook' => 'add_attachment'
			],
			'edit_attachment'             => [
				'label' => 'Attachment Edit',
				'hook' => 'edit_attachment'
			],
			'save_attachment'             => [
				'label' => 'Attachment Save',
				'hook' => 'attachment_fields_to_save'
			],
			'attachment_updated'          => [
				'label' => 'Attachment Update',
				'hook' => 'attachment_updated'
			],
			'attachment_count'            => [
				'label' => 'Attachment Count',
				'hook' => 'wp_count_attachments'
			],
			'attachment_metadata'         => [
				'label' => 'Generate Attachment Metadata',
				'hook' => 'wp_generate_attachment_metadata'
			],
			'delete_attachment'           => [
				'label' => 'Media Deletion',
				'hook' => 'delete_attachment'
			],
			'media_edit'                  => [
				'label' => 'Media Edit',
				'hook' => 'edit_attachment'
			],
			'media_upload_tabs'           => [
				'label' => 'Media Tabs',
				'hook' => 'media_upload_tabs'
			],
			'image_sizes'                 => [
				'label' => 'Image Sizes',
				'hook' => 'image_size_names_choose'
			],
			'user_register'          => [
				'label' => 'User Registered',
				'hook' => 'user_register'
			],
			'set_user_role'          => [
				'label' => 'User Role Updated',
				'hook' => 'set_user_role'
			],
			'profile_update'         => [
				'label' => 'Profile Updated',
				'hook' => 'profile_update'
			],
			'wp_update_user'         => [
				'label' => 'Update User',
				'hook' => 'wp_update_user'
			],
			'remove_user_from_blog'  => [
				'label' => 'Remove Blog User',
				'hook' => 'remove_user_from_blog'
			],
			'delete_user'            => [
				'label' => 'Delete User',
				'hook' => 'delete_user'
			],
			'wpmu_delete_user'       => [
				'label' => 'Delete User (MU)',
				'hook' => 'wpmu_delete_user'
			],
			'wpmu_new_user'          => [
				'label' => 'New User Created',
				'hook' => 'wpmu_new_user'
			],
			'wpmu_activate_user'     => [
				'label' => 'Activate User',
				'hook' => 'wpmu_activate_user'
			],
			'create_application_password' => [
				'label' => 'Create Application Password',
				'hook' => 'wp_create_application_password'
			],
			'update_application_password' => [
				'label' => 'Update Application Password',
				'hook' => 'wp_update_application_password'
			],
			'delete_application_password' => [
				'label' => 'Delete Application Password',
				'hook' => 'wp_delete_application_password'
			],
			'add_user_role'               => [
				'label' => 'User Added to a Role',
				'hook' => 'add_user_role'
			],

			'wp_login'               => [
				'label' => 'User Logged In',
				'hook' => 'wp_login'
			],
			'wp_login_failed'        => [
				'label' => 'Login Failed',
				'hook' => 'wp_login_failed'
			],
			'wp_logout'              => [
				'label' => 'User Logged Out',
				'hook' => 'wp_logout'
			],
			'validate_reset'              => [
				'label' => 'Validate Reset',
				'hook' => 'validate_password_reset'
			],
			'comment_post'           => [
				'label' => 'Comment Added',
				'hook' => 'comment_post'
			],
			'wp_insert_comment'      => [
				'label' => 'Comment Created',
				'hook' => 'wp_insert_comment'
			],
			'edit_comment'           => [
				'label' => 'Edit Comment',
				'hook' => 'edit_comment'
			],
			'trashed_comment'        => [
				'label' => 'Comment Trashed',
				'hook' => 'trashed_comment'
			],
			'untrashed_comment'      => [
				'label' => 'Comment Untrashed',
				'hook' => 'untrashed_comment'
			],
			'transition_comment_status' => [
				'label' => 'Comment Status Changed',
				'hook' => 'transition_comment_status'
			],
			'pre_comment_approved'   => [
				'label' => 'Comment Pre-Approval',
				'hook'  => 'pre_comment_approved',
			],
			'delete_comment'         => [
				'label' => 'Delete Comment',
				'hook' => 'delete_comment'
			],
			'create_term'            => [
				'label' => 'Create Term',
				'hook' => 'create_term'
			],
			'created_term'           => [
				'label' => 'Term Created',
				'hook' => 'created_term'
			],
			'edit_term'              => [
				'label' => 'Edit Term',
				'hook' => 'edit_term'
			],
			'edited_term'            => [
				'label' => 'Term Edited',
				'hook' => 'edited_term'
			],
			'saved_term'             => [
				'label' => 'Term Updated',
				'hook' => 'saved_term'
			],
			'delete_term'            => [
				'label' => 'Delete Term',
				'hook' => 'delete_term'
			],

			'activated_plugin'      => [
				'label' => 'Activate Plugin',
				'hook' => 'activated_plugin'
			],
			'deactivate_plugin'     => [
				'label' => 'Deactivate Plugin',
				'hook' => 'deactivate_plugin'
			],
			'switch_theme'                => [
				'label' => 'Theme Switch',
				'hook' => 'switch_theme'
			],

			'add_option'             => [
				'label' => 'Add Option',
				'hook' => 'add_option'
			],
			'update_option'          => [
				'label' => 'Update Option',
				'hook' => 'update_option'
			],
			'delete_option'          => [
				'label' => 'Delete Option',
				'hook' => 'delete_option'
			],
			'upgrader_process_complete' => [
				'label' => 'Upgrader Complete',
				'hook' => 'upgrader_process_complete'
			],
			'generate_rewrite_rules' => [
				'label' => 'Rewrite Rules Generated',
				'hook' => 'generate_rewrite_rules'
			],
			'switch_blog'            => [
				'label' => 'Blog Switch',
				'hook' => 'switch_blog'
			],
			'customize_register'     => [
				'label' => 'Customizer Registration',
				'hook' => 'customize_register'
			],
			'rest_api_init'          => [
				'label' => 'REST API Init',
				'hook' => 'rest_api_init'
			],
			'update_blog_public'  => [
				'label' => 'Update Blog Public',
				'hook' => 'update_blog_public'
			],
			'update_blog_status' => [
				'label' => 'Update Blog Status',
				'hook' => 'update_blog_status'
			],
			'wpmu_new_blog' => [
				'label' => 'New Blog',
				'hook' => 'wpmu_new_blog'
			],
		];
	}

	/**
	 * Realistic sample payloads for the "@" variable picker.
	 *
	 * Every trigger returned by get_triggers() must yield a non-empty array so
	 * fields can be mapped before a live capture. Field keys mirror the shapes
	 * resolve_trigger() actually emits (Post/User/Comment/term toArray columns
	 * plus the extra keys merged per event). Explicit samples come first, then
	 * category fallbacks by event group keep any unlisted trigger non-empty.
	 */
	public static function get_trigger_sample_output( string $event ): array {
		// Shared base payloads reused across the category samples/fallbacks.
		$post_sample = [
			'ID'             => 101,
			'post_author'    => 1,
			'post_date'      => '2024-01-01 10:00:00',
			'post_date_gmt'  => '2024-01-01 10:00:00',
			'post_content'   => 'Post body content.',
			'post_title'     => 'Sample Post',
			'post_excerpt'   => 'Sample excerpt.',
			'post_status'    => 'publish',
			'comment_status' => 'open',
			'ping_status'    => 'open',
			'post_name'      => 'sample-post',
			'post_modified'  => '2024-01-01 10:00:00',
			'post_parent'    => 0,
			'guid'           => 'https://example.com/?p=101',
			'menu_order'     => 0,
			'post_type'      => 'post',
			'post_mime_type' => '',
			'comment_count'  => 0,
		];

		$media_sample = array_merge( $post_sample, [
			'ID'             => 202,
			'post_title'     => 'Sample Image',
			'post_name'      => 'sample-image',
			'post_status'    => 'inherit',
			'post_type'      => 'attachment',
			'post_mime_type' => 'image/png',
			'guid'           => 'https://example.com/wp-content/uploads/sample-image.png',
			'url'            => 'https://example.com/wp-content/uploads/sample-image.png',
			'user_id'        => 1,
			'time'           => '2024-01-01 10:00:00',
		] );

		$user_sample = [
			'ID'              => 5,
			'user_login'      => 'johndoe',
			'user_nicename'   => 'johndoe',
			'user_email'      => 'john@example.com',
			'user_url'        => 'https://example.com',
			'user_registered' => '2024-01-01 10:00:00',
			'user_status'     => 0,
			'display_name'    => 'John Doe',
		];

		$comment_sample = [
			'comment_ID'           => 55,
			'comment_post_ID'      => 101,
			'comment_author'       => 'Jane Reader',
			'comment_author_email' => 'jane@example.com',
			'comment_author_url'   => 'https://example.com',
			'comment_author_IP'    => '127.0.0.1',
			'comment_date'         => '2024-01-01 10:00:00',
			'comment_date_gmt'     => '2024-01-01 10:00:00',
			'comment_content'      => 'Great post!',
			'comment_karma'        => 0,
			'comment_approved'     => '1',
			'comment_agent'        => 'Mozilla/5.0',
			'comment_type'         => 'comment',
			'comment_parent'       => 0,
			'user_id'              => 5,
		];

		$term_sample = [
			'term_id'          => 3,
			'term_taxonomy_id' => 3,
			'taxonomy'         => 'category',
			'name'             => 'News',
			'slug'             => 'news',
			'description'      => 'News category.',
			'parent'           => 0,
			'count'            => 12,
		];

		$option_sample = [
			'option_name' => 'blogname',
			'value'       => 'My Site',
		];

		$blog_sample = [
			'blog_id'    => 2,
			'blog_url'   => 'https://example.com/site2',
			'blog_name'  => 'Second Site',
		];

		// Explicit samples: events whose payload has extra/merged keys.
		$samples = [
			// Posts.
			'transition_post_status' => array_merge( $post_sample, [
				'old_status' => 'draft',
				'new_status' => 'publish',
			] ),
			'wp_insert_post'         => array_merge( $post_sample, [
				'post_type'   => 'revision',
				'revision_id' => 102,
				'parent'      => $post_sample,
			] ),
			'wp_after_insert_post'   => array_merge( $post_sample, [ 'is_update' => true ] ),

			// Media / attachments.
			'save_attachment'     => array_merge( $media_sample, [ 'fields' => [] ] ),
			'attachment_metadata' => array_merge( $media_sample, [
				'metadata' => [
					'width'  => 1200,
					'height' => 800,
					'file'   => '2024/01/sample-image.png',
				],
			] ),
			'attachment_count'    => [
				'post_type' => 'attachment',
				'counts'    => [ 'image/png' => 5 ],
				'time'      => '2024-01-01 10:00:00',
			],
			'delete_attachment'   => [
				'attachment_id' => 202,
				'user_id'       => 1,
				'time'          => '2024-01-01 10:00:00',
			],
			'media_upload_tabs'   => [
				'tabs'         => [
					'type' => 'From Computer',
					'library' => 'Media Library'
				],
				'tabs_keys'    => [ 'type', 'library' ],
				'count'        => 2,
				'triggered_at' => '2024-01-01 10:00:00',
			],
			'image_sizes'         => [
				'sizes'      => [
					'thumbnail' => 'Thumbnail',
					'medium' => 'Medium'
				],
				'sizes_keys' => [ 'thumbnail', 'medium' ],
				'count'      => 2,
				'time'       => '2024-01-01 10:00:00',
			],

			// Users.
			'wp_login'                    => array_merge( $user_sample, [ 'roles' => [ 'subscriber' ] ] ),
			'validate_reset'              => array_merge( $user_sample, [ 'roles' => [ 'subscriber' ] ] ),
			'wp_login_failed'             => [
				'username' => 'johndoe',
				'failed'   => true,
			],
			'wp_authenticate'             => [
				'username' => 'johndoe',
				'password' => '',
			],
			'create_application_password' => array_merge( $user_sample, [
				'new_password' => 'abcd 1234 efgh 5678',
				'time'         => '2024-01-01 10:00:00',
			] ),
			'update_application_password' => array_merge( $user_sample, [
				'item_name' => 'My App',
				'item_id'   => '0b7f1e8a-1234-5678-9abc-def012345678',
				'time'      => '2024-01-01 10:00:00',
			] ),
			'delete_application_password' => array_merge( $user_sample, [
				'uuid' => '0b7f1e8a-1234-5678-9abc-def012345678',
			] ),

			// Comments.
			'transition_comment_status' => array_merge( $comment_sample, [
				'old_status' => 'hold',
				'new_status' => 'approve',
			] ),
			'pre_comment_approved'      => [
				'approved'   => 1,
				'comment_id' => 55,
			],

			// Plugins / themes.
			'activated_plugin'  => [ 'plugin' => 'akismet/akismet.php' ],
			'deactivate_plugin' => [ 'plugin' => 'akismet/akismet.php' ],
			'switch_theme'      => [ 'theme' => 'Twenty Twenty-Four' ],

			// Options.
			'add_option'    => $option_sample,
			'update_option' => $option_sample,
			'delete_option' => $option_sample,

			// System / multisite.
			'upgrader_process_complete' => [
				'action' => 'update',
				'type'   => 'plugin',
			],
			'generate_rewrite_rules'    => [ 'event' => 'generate_rewrite_rules' ],
			'switch_blog'               => $blog_sample,
			'customize_register'        => [
				'message' => 'Customize Registration',
				'time'    => '2024-01-01 10:00:00',
			],
			'rest_api_init'             => [ 'time' => '2024-01-01 10:00:00' ],
			'update_blog_public'        => [
				'blog_id'   => 2,
				'is_public' => 1,
			],
			'update_blog_status'        => [
				'blog_id'    => 2,
				'new_status' => 1,
				'old_status' => 0,
			],
			'wpmu_new_blog'             => [
				'blog_id' => 2,
				'user_id' => 1,
				'domain'  => 'example.com',
				'path'    => '/site2/',
				'site_id' => 1,
				'meta'    => [],
			],
		];

		if ( isset( $samples[ $event ] ) ) {
			return $samples[ $event ];
		}

		// Category fallbacks by event group so every trigger stays non-empty and
		// exposes the field shape resolve_trigger() emits before a live capture.
		$post_events = [
			'publish_post',
			'post_updated',
			'wp_trash_post',
			'untrashed_post',
			'delete_post',
			'deleted_post',
			'save_post',
			'post_revision',
		];
		if ( in_array( $event, $post_events, true ) || 0 === strpos( $event, 'post_' ) ) {
			return $post_sample;
		}

		if ( false !== strpos( $event, 'attachment' ) || 0 === strpos( $event, 'media' )
			|| 0 === strpos( $event, 'image' ) ) {
			return $media_sample;
		}

		$user_events = [
			'user_register',
			'set_user_role',
			'add_user_role',
			'profile_update',
			'wp_update_user',
			'remove_user_from_blog',
			'delete_user',
			'wpmu_delete_user',
			'wpmu_new_user',
			'wpmu_activate_user',
			'wp_logout',
		];
		if ( in_array( $event, $user_events, true ) || 0 === strpos( $event, 'user_' )
			|| false !== strpos( $event, 'application_password' ) ) {
			return $user_sample;
		}

		if ( 0 === strpos( $event, 'comment' ) || false !== strpos( $event, '_comment' ) ) {
			return $comment_sample;
		}

		if ( false !== strpos( $event, 'term' ) ) {
			return $term_sample;
		}

		if ( false !== strpos( $event, 'option' ) ) {
			return $option_sample;
		}

		if ( false !== strpos( $event, 'plugin' ) ) {
			return [ 'plugin' => 'akismet/akismet.php' ];
		}

		if ( false !== strpos( $event, 'theme' ) ) {
			return [ 'theme' => 'Twenty Twenty-Four' ];
		}

		if ( false !== strpos( $event, 'blog' ) ) {
			return $blog_sample;
		}

		return [ 'time' => '2024-01-01 10:00:00' ];
	}

	public static function get_trigger_config_schema( string $trigger ): array {
		if ( 'publish_post' === $trigger ) {
			return [
				[
					'key'   => 'post_type',
					'label' => 'Post Type',
					'type'  => 'select',
					'dynamic' => [
						'integration' => 'wordpress',
						'query'       => 'post_types',
						'select'      => [ 'name', 'label' ],
					],
					'required' => true,
				],
				[
					'key'   => 'post_status',
					'label' => 'Post Status',
					'type'  => 'select',
					'required' => true,
					'options' => [
						[
							'label' => 'Publish',
							'value' => 'publish'
						],
						[
							'label' => 'Draft',
							'value' => 'draft'
						],
					]
				]
			];
		}//end if

		if ( 'post_updated' === $trigger ) {
			return [
				[
					'key'   => 'post_type',
					'label' => 'Post Type',
					'type'  => 'select',
					'dynamic' => [
						'integration' => 'wordpress',
						'query'       => 'post_types',
						'select'      => [ 'name', 'label' ],
					],
					'required' => true,
				],
				[
					'key'   => 'post',
					'label' => 'Posts',
					'type'  => 'select',
					'dynamic' => [
						'integration' => 'wordpress',
						'query'       => 'posts',
						'select'      => [ 'name', 'label' ],
						'depends_on'  => [ 'post_type' ],
					],
					'required' => true,
				],
				[
					'key'   => 'post_status',
					'label' => 'Post Status',
					'type'  => 'select',
					'required' => true,
					'options' => [
						[
							'label' => 'Publish',
							'value' => 'publish'
						],
						[
							'label' => 'Draft',
							'value' => 'draft'
						],
					]
				]
			];
		}//end if

		if ( in_array( $trigger, [ 'trashed_comment', 'untrashed_comment' ], true ) ) {
			return [
				[
					'key' => 'post_type',
					'label' => 'Post Type',
					'type' => 'select',
					'dynamic' => [
						'integration' => 'wordpress',
						'query' => 'post_types',
						'select' => [ 'name', 'label' ],
					],
					'required' => true,
				],
			];
		}

		if ( 'transition_comment_status' === $trigger ) {
			return [
				[
					'key' => 'from_status',
					'label' => 'From Status',
					'type' => 'select',
					'required' => true,
					'options' => [
						[
							'label' => 'Approved',
							'value' => 'approve'
						],
						[
							'label' => 'Pending',
							'value' => 'pending'
						],
						[
							'label' => 'Spam',
							'value' => 'spam'
						],
						[
							'label' => 'Trash',
							'value' => 'trash'
						],
					]
				],
				[
					'key' => 'to_status',
					'label' => 'To Status',
					'type' => 'select',
					'required' => true,
					'options' => [
						[
							'label' => 'Approved',
							'value' => 'approve'
						],
						[
							'label' => 'Pending',
							'value' => 'pending'
						],
						[
							'label' => 'Spam',
							'value' => 'spam'
						],
						[
							'label' => 'Trash',
							'value' => 'trash'
						],
					]
				],
			];
		}//end if
		if ( 'do_action' === $trigger ) {
			return [
				[
					'key' => 'hook_name',
					'label' => "Hook Name (Use this code to trigger the action: do_action('hook_name', 1, ['key' => 'value']))",
					'type' => 'text',
					'required' => true,
				],
			];
		}

		if ( 'activated_plugin' === $trigger ) {
			return [
				[
					'key'     => 'plugin',
					'label'   => 'Inactive Plugin',
					'type'    => 'select',
					'dynamic' => [
						'integration' => 'wordpress',
						'query'       => 'inactive_plugins',
						'select'      => [ 'file', 'name' ],
					],
					'required' => true,
				],
			];
		}

		if ( 'switch_theme' === $trigger ) {
			return [
				[
					'key'     => 'theme',
					'label'   => 'Theme Switch',
					'type'    => 'select',
					'dynamic' => [
						'integration' => 'wordpress',
						'query'       => 'deactivate_theme',
						'select'      => [ 'file', 'name' ],
					],
					'required' => true,
				],
			];
		}

		if ( 'add_action' === $trigger ) {
			return [
				[
					'key'      => 'hook_name',
					'label'    => 'Hook Name',
					'type'     => 'text',
					'required' => true,
				],
			];
		}

		if ( in_array( $trigger, self::get_user_filterable_triggers(), true ) ) {
			return self::field_trigger_user_filter();
		}

		if ( in_array( $trigger, self::get_term_filterable_triggers(), true ) ) {
			return self::field_trigger_term_filter();
		}

		return [];
	}

	private static function get_user_filterable_triggers(): array {
		return [
			'user_register',
			'set_user_role',
			'profile_update',
			'wp_update_user',
			'remove_user_from_blog',
			'delete_user',
			'wpmu_delete_user',
			'wpmu_new_user',
			'wpmu_activate_user',
			'create_application_password',
			'update_application_password',
			'delete_application_password',
			'add_user_role',
			'wp_login',
			'validate_reset',
			'wp_logout',
		];
	}

	private static function field_trigger_user_filter(): array {
		return [
			[
				'key' => 'user_id',
				'label' => 'User',
				'type' => 'select',
				'dynamic' => [
					'integration' => 'wordpress',
					'query'       => 'users_with_any',
					'select'      => [ 'ID', 'name' ],
				],
				'default' => 'any',
				'required' => true,
			],
		];
	}

	private static function get_term_filterable_triggers(): array {
		return [
			'create_term',
			'edit_term',
			'edited_term',
			'saved_term',
			'delete_term',
		];
	}

	private static function field_trigger_term_filter(): array {
		return [
			[
				'key' => 'term_id',
				'label' => 'Term',
				'type' => 'select',
				'dynamic' => [
					'integration' => 'wordpress',
					'query'       => 'terms_with_any',
					'select'      => [ 'term_id', 'name' ],
				],
				'default' => 'any',
				'required' => true,
			],
		];
	}

	private static function get_trigger_node_config( array $node ): array {
		if ( isset( $node['config'] ) && is_array( $node['config'] ) ) {
			return $node['config'];
		}

		$data = $node['data'] ?? [];
		if ( isset( $data['config'] ) && is_array( $data['config'] ) ) {
			return $data['config'];
		}

		return [];
	}

	private static function get_payload_user_id( array $payload ): int {
		$keys = [ 'ID', 'id', 'user_id', 'userId' ];
		foreach ( $keys as $key ) {
			if ( isset( $payload[ $key ] ) ) {
				return (int) $payload[ $key ];
			}
		}

		return 0;
	}

	private static function get_payload_term_id( array $payload ): int {
		$keys = [ 'term_id', 'id', 'termId' ];
		foreach ( $keys as $key ) {
			if ( isset( $payload[ $key ] ) ) {
				return (int) $payload[ $key ];
			}
		}

		return 0;
	}

	private static function filter_user_trigger_payload( array $node, $payload ) {
		if ( ! is_array( $payload ) ) {
			return false;
		}

		$event = $node['event'] ?? ( $node['data']['event'] ?? '' );
		if ( ! in_array( $event, self::get_user_filterable_triggers(), true ) ) {
			return $payload;
		}

		$config = self::get_trigger_node_config( $node );
		$selected_user_id = strtolower( trim( (string) ( $config['user_id'] ?? 'any' ) ) );
		if ( '' === $selected_user_id || 'any' === $selected_user_id || 'all' === $selected_user_id ) {
			return $payload;
		}

		$payload_user_id = self::get_payload_user_id( $payload );
		if ( $payload_user_id <= 0 ) {
			return false;
		}

		return ( (int) $selected_user_id === $payload_user_id ) ? $payload : false;
	}

	private static function filter_term_trigger_payload( array $node, $payload ) {
		if ( ! is_array( $payload ) ) {
			return false;
		}

		$event = $node['event'] ?? ( $node['data']['event'] ?? '' );
		if ( ! in_array( $event, self::get_term_filterable_triggers(), true ) ) {
			return $payload;
		}

		$config = self::get_trigger_node_config( $node );
		$selected_term_id = strtolower( trim( (string) ( $config['term_id'] ?? 'any' ) ) );
		if ( '' === $selected_term_id || 'any' === $selected_term_id || 'all' === $selected_term_id ) {
			return $payload;
		}

		$payload_term_id = self::get_payload_term_id( $payload );
		if ( $payload_term_id <= 0 ) {
			return false;
		}

		return ( (int) $selected_term_id === $payload_term_id ) ? $payload : false;
	}

	private static function resolve_post_payload( int $post_id ) {
		$post = Post::find( $post_id );
		if ( ! $post ) {
			return false;
		}

		return $post->toArray();
	}

	private static function resolve_media_payload( int $attachment_id ) {
		if ( ! $attachment_id ) {
			return false;
		}

		$attachment = Post::find( $attachment_id );
		if ( ! $attachment || 'attachment' !== $attachment->post_type ) {
			return false;
		}

		$currentUser = User::current();

		return array_merge($attachment->toArray(), [
			'url'     => wp_get_attachment_url( $attachment_id ),
			'user_id' => $currentUser ? $currentUser->ID : 0,
			'time'    => current_time( 'mysql' ),
		]);
	}

	private static function resolve_comment_payload( int $comment_id ) {
		$comment = Comment::find( $comment_id );
		if ( ! $comment ) {
			return false;
		}

		return $comment->toArray();
	}

	public static function resolve_trigger( array $node, array $args ) {

		$config = $node['config'] ?? [];

		switch ( $node['event'] ) {
			case 'publish_post':
			case 'delete_post':
			case 'untrashed_post':
			case 'wp_trash_post':
			case 'deleted_post':
			case 'save_post':
				return self::resolve_post_payload( $args[0] ?? 0 );

			case 'post_updated':
				$post_id = isset( $args[0] ) ? (int) $args[0] : 0;

				if ( ! $post_id ) {
					return false;
				}

				if ( $parent_id = wp_is_post_revision( $post_id ) ) {
					$post_id = $parent_id;
				}

				$post = get_post( $post_id );
				if ( ! $post ) {
					return false;
				}

				if (
					( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) ||
					wp_is_post_revision( $post_id ) ||
					$post->post_status === 'auto-draft'
				) {
					return false;
				}

				$selected_type = $config['post_type'] ?? 'post';
				$selected_id   = $config['post'] ?? null;
				$current_type  = ( $post->post_type === 'attachment' ) ? 'media' : $post->post_type;

				if ( $selected_type !== $current_type ) {
					return false;
				}

				if ( ! empty( $selected_id ) && $selected_id !== 'any' && (int) $selected_id !== $post_id ) {
					return false;
				}

				return self::resolve_post_payload( $post_id );

			case 'transition_post_status':
				$wpPost = $args[2] ?? null;
				$postId = $wpPost instanceof \WP_Post ? $wpPost->ID : (int) $wpPost;
				$post = Post::find( $postId );
				if ( ! $post || ( $args[1] ?? '' ) === 'new' ) {
					return false;
				}

				return array_merge($post->toArray(), [
					'old_status' => $args[1] ?? '',
					'new_status' => $args[0] ?? '',
				]);

			case 'wp_insert_post':
				$post = Post::find( $args[0] ?? 0 );
				if ( ! $post || 'revision' !== $post->post_type ) {
					return false;
				}

				$parent = Post::find( $post->post_parent );
				return array_merge($post->toArray(), [
					'revision_id' => $post->ID,
					'parent'      => $parent ? $parent->toArray() : null,
				]);

			case 'wp_after_insert_post':
				$post = Post::find( $args[0] ?? 0 );
				if ( ! $post ) {
					return false;
				}

				return array_merge(
					self::resolve_post_payload( $post->ID ),
					[ 'is_update' => $args[1] ?? false ]
				);

			case 'update_blog_status':
				$blog_id    = $args[0] ?? 0;
				$new_status = $args[1] ?? 0;
				$old_status = $args[2] ?? 0;
				if ( ! $blog_id ) {
					return false;
				}

				return [
					'blog_id' => $blog_id,
					'new_status' => $new_status,
					'old_status' => $old_status,
				];

			case 'add_attachment':
			case 'edit_attachment':
			case 'attachment_updated':
			case 'media_edit':
				return self::resolve_media_payload( $args[0] ?? 0 );

			case 'save_attachment':
				$post_id = $args[0] ?? 0;
				$attachment_fields = $args[1] ?? [];
				if ( ! $post_id ) {
					return false;
				}

				$attachment = Post::find( $post_id );
				if ( ! $attachment || 'attachment' !== $attachment->post_type ) {
					return false;
				}

				return array_merge($attachment->toArray(), [
					'url'     => wp_get_attachment_url( $post_id ),
					'user_id' => get_current_user_id(),
					'time'    => current_time( 'mysql' ),
					'fields'  => $attachment_fields,
				]);

			case 'attachment_count':
				$post_type = $args[0] ?? 0;
				$count = wp_count_attachments( $post_type );

				return [
					'post_type' => $post_type,
					'counts'    => (array) $count,
					'time'     => current_time( 'mysql' ),
				];

			case 'attachment_metadata':
				$metadata = $args[0] ?? [];
				$attachment_id = $args[1] ?? 0;
				if ( ! $attachment_id || empty( $metadata ) ) {
					return false;
				}

				$attachment = Post::find( $attachment_id );
				if ( ! $attachment || 'attachment' !== $attachment->post_type ) {
					return false;
				}

				return array_merge($attachment->toArray(), [
					'url'      => wp_get_attachment_url( $attachment_id ),
					'metadata' => $metadata,
					'user_id'  => get_current_user_id(),
					'time'     => current_time( 'mysql' ),
				]);

			case 'delete_attachment':
				$attachment_id = $args[0] ?? 0;
				if ( ! $attachment_id ) {
					return false;
				}

				return [
					'attachment_id' => $attachment_id,
					'user_id'       => get_current_user_id(),
					'time'          => current_time( 'mysql' ),
				];

			case 'media_upload_tabs':
				$tabs = $args[0] ?? [];
				if ( empty( $tabs ) || ! is_array( $tabs ) ) {
					return false;
				}

				return [
					'tabs'         => $tabs,
					'tabs_keys'    => array_keys( $tabs ),
					'count'        => count( $tabs ),
					'triggered_at' => current_time( 'mysql' ),
				];

			case 'image_sizes':
				$sizes = $args[0] ?? [];
				if ( empty( $sizes ) || ! is_array( $sizes ) ) {
					return false;
				}

				return [
					'sizes'      => $sizes,
					'sizes_keys' => array_keys( $sizes ),
					'count'      => count( $sizes ),
					'time'       => current_time( 'mysql' ),
				];

			case 'comment_post':
			case 'edit_comment':
			case 'delete_comment':
			case 'trashed_comment':
			case 'untrashed_comment':
			case 'wp_insert_comment':
				return self::resolve_comment_payload( $args[0] ?? 0 );

			case 'transition_comment_status':
				$new_status = $args[0] ?? '';
				$comment    = $args[1] ?? null;
				$old_status = $args[2] ?? '';

				$comment_data = [];
				if ( $comment instanceof \WP_Comment ) {
					$comment_data = $comment->to_array();
				} elseif ( is_numeric( $comment ) ) {
					$payload = self::resolve_comment_payload( (int) $comment );
					if ( is_array( $payload ) ) {
						$comment_data = $payload;
					}
				}

				return array_merge( $comment_data, [
					'old_status' => $old_status,
					'new_status' => $new_status,
				] );

			case 'pre_comment_approved':
				return [
					'approved'   => $args[0] ?? null,
					'comment_id' => $args[1] ?? 0,
				];

			case 'wp_set_comment_status':
				return self::resolve_comment_payload( $args[0] ?? 0 );

			case 'activated_plugin':
			case 'deactivate_plugin':
				$plugin = $args[0] ?? 0;
				if ( ! $plugin ) {
					return false;
				}

				return [
					'plugin' => $plugin,
				];

			case 'switch_theme':
				$theme = $args[0] ?? 0;
				if ( ! $theme ) {
					return false;
				}

				return [
					'theme' => $theme,
				];

			case 'user_register':
			case 'profile_update':
			case 'set_user_role':
			case 'add_user_role':
			case 'remove_user_from_blog':
			case 'delete_user':
			case 'wpmu_delete_user':
			case 'wp_update_user':
			case 'wpmu_new_user':
			case 'wpmu_activate_user':
				$payload = self::get_user_payload( $args[0] ?? 0 );
				return self::filter_user_trigger_payload( $node, $payload );

			case 'wp_login':
			case 'validate_reset':
				$wpUser = $args[1] ?? null;
				if ( ! $wpUser instanceof \WP_User ) {
					return false;
				}

				$user = User::find( $wpUser->ID );
				if ( ! $user ) {
					return false;
				}

				$payload = array_merge($user->toArray(), [
					'roles' => $wpUser->roles,
				]);

				return self::filter_user_trigger_payload( $node, $payload );

			case 'wp_login_failed':
				return [
					'username' => $args[0] ?? '',
					'failed'   => true,
				];

			case 'wp_logout':
				$currentUser = User::current();
				if ( ! $currentUser ) {
					return false;
				}

				return self::filter_user_trigger_payload( $node, $currentUser->toArray() );

			case 'create_application_password':
				$user_id      = $args[0] ?? 0;
				$new_password = $args[1] ?? '';
				if ( ! $user_id || empty( $new_password ) ) {
					return false;
				}
				$user = User::find( $user_id );
				if ( ! $user ) {
					return false;
				}

				$payload = array_merge($user->toArray(), [
					'new_password' => $new_password,
					'time'         => current_time( 'mysql' ),
				]);

				return self::filter_user_trigger_payload( $node, $payload );

			case 'update_application_password':
				$user_id = $args[0] ?? 0;
				$item    = $args[1] ?? null;
				if ( ! $user_id || empty( $item ) ) {
					return false;
				}

				$user = User::find( $user_id );
				if ( ! $user ) {
					return false;
				}

				$payload = array_merge($user->toArray(), [
					'item_name' => $item['name'] ?? '',
					'item_id'   => $item['uuid'] ?? '',
					'time'      => current_time( 'mysql' ),
				]);

				return self::filter_user_trigger_payload( $node, $payload );

			case 'delete_application_password':
				$user_id = $args[0] ?? 0;
				$uuid    = $args[1] ?? '';
				if ( ! $user_id ) {
					return false;
				}

				$user = User::find( $user_id );
				if ( ! $user ) {
					return false;
				}

				$payload = array_merge($user->toArray(), [
					'uuid' => $uuid,
				]);

				return self::filter_user_trigger_payload( $node, $payload );

			case 'create_term':
			case 'created_term':
			case 'edit_term':
			case 'edited_term':
			case 'saved_term':
			case 'delete_term':
				$payload = self::get_term_payload(
					$args[0] ?? 0,
					$args[2] ?? '',
					$args[1] ?? 0
				);
				return self::filter_term_trigger_payload( $node, $payload );

			case 'add_option':
			case 'update_option':
			case 'delete_option':
				return [
					'option_name' => $args[0] ?? '',
					'value'       => $args[2] ?? null,
				];

			case 'upgrader_process_complete':
				return [
					'action' => $args[1]['action'] ?? '',
					'type'   => $args[1]['type'] ?? '',
				];

			case 'generate_rewrite_rules':
				return [ 'event' => $node['event'] ];

			case 'switch_blog':
				$blog = $args[0] ?? 0;
				if ( ! $blog ) {
					return false;
				}

				return [
					'blog_id'   => $blog,
					'blog_url'  => get_home_url( $blog ),
					'blog_name' => get_blog_option( $blog, 'blogname' ),
				];

			case 'customize_register':
				$customize = $args[0] ?? null;
				if ( ! $customize ) {
					return false;
				}

				return [
					'message' => 'Customize Registration',
					'time'    => current_time( 'mysql' ),
				];

			case 'rest_api_init':
				return [
					'time' => current_time( 'mysql' ),
				];

			case 'update_blog_public':
				$blog_id = $args[0] ?? 0;
				$public  = $args[1] ?? 0;
				if ( ! $blog_id ) {
					return false;
				}

				return [
					'blog_id' => $blog_id,
					'is_public' => $public,
				];

			case 'update_blog_status':
				$blog_id    = $args[0] ?? 0;
				$new_status = $args[1] ?? 0;
				$old_status = $args[2] ?? 0;
				if ( ! $blog_id ) {
					return false;
				}

				return [
					'blog_id' => $blog_id,
					'new_status' => $new_status,
					'old_status' => $old_status,
				];

			case 'new_blog':
				$blog_id = $args[0] ?? 0;
				$user_id = $args[1] ?? 0;
				$domain  = $args[2] ?? '';
				$path    = $args[3] ?? '';
				$site_id = $args[4] ?? 0;
				$meta    = $args[5] ?? [];
				if ( ! $blog_id ) {
					return false;
				}

				return [
					'blog_id' => $blog_id,
					'user_id' => $user_id,
					'domain'  => $domain,
					'path'    => $path,
					'site_id' => $site_id,
					'meta'    => $meta,
				];
		}//end switch

		return false;
	}

	public static function query_users_with_any( $q ): array {
		return array_merge(
			[
				[
					'ID' => 'any',
					'name' => 'All Users',
				],
			],
			self::query_users( $q )
		);
	}

	public static function query_terms_with_any( $q ): array {
		return array_merge(
			[
				[
					'term_id' => 'any',
					'name'    => 'All Terms',
				],
			],
			self::query_terms( $q )
		);
	}

	public static function get_actions(): array {
		return [
			'create_post'                   => [ 'label' => 'Create Post' ],
			'update_option'                 => [ 'label' => 'Update Option' ],
			'create_user'                   => [ 'label' => 'Create User' ],
			'update_user'                   => [ 'label' => 'Update User' ],
			'delete_user'                   => [ 'label' => 'Delete User' ],
			'get_users'                     => [ 'label' => 'Get All Users' ],
			'get_users_by_role'             => [ 'label' => 'Get All Users by Role' ],
			'get_user_by_id'                => [ 'label' => 'Get User by ID' ],
			'get_user_by_email'             => [ 'label' => 'Get User by Email' ],
			'get_user_by_field'             => [ 'label' => 'Get User by Field' ],
			'get_user_meta_all'             => [ 'label' => 'Get User Metadata (All)' ],
			'get_user_meta_single'          => [ 'label' => 'Get User Metadata (Single)' ],
			'update_user_meta'              => [ 'label' => 'Update User Metadata' ],
			'send_password_reset_email'     => [ 'label' => 'Send Password Reset Email' ],
			'authenticate_user'             => [ 'label' => 'Authenticate User' ],
			'logout_user'                   => [ 'label' => 'Logout User' ],
			'activate_user'                 => [ 'label' => 'Activate User' ],
			'deactivate_user'               => [ 'label' => 'Deactivate User' ],
			'create_role'                   => [ 'label' => 'Create Role' ],
			'delete_role'                   => [ 'label' => 'Delete Role' ],
			'add_user_role'                 => [ 'label' => 'Add User Role' ],
			'remove_user_role'              => [ 'label' => 'Remove User Role' ],
			'update_user_role'              => [ 'label' => 'Update User Role' ],
			'get_roles'                     => [ 'label' => 'Get All Roles' ],
			'get_caps'                      => [ 'label' => 'Get All Capabilities' ],
			'get_role_caps'                 => [ 'label' => 'Get Role Capabilities' ],
			'add_role_caps'                 => [ 'label' => 'Add Role Capabilities' ],
			'remove_role_caps'              => [ 'label' => 'Remove Role Capabilities' ],
			'get_user_caps'                 => [ 'label' => 'Get User Capabilities' ],
			'add_user_caps'                 => [ 'label' => 'Add User Capabilities' ],
			'remove_user_caps'              => [ 'label' => 'Remove User Capabilities' ],
			'get_term'                      => [ 'label' => 'Get Term (Single)' ],
			'get_terms_by_taxonomy'         => [ 'label' => 'Get Term by Taxonomy' ],
			'get_term_by_field'             => [ 'label' => 'Get Term by Field' ],
			'create_term'                   => [ 'label' => 'Create New Term' ],
			'update_term'                   => [ 'label' => 'Update Term' ],
			'delete_term'                   => [ 'label' => 'Delete Term' ],
			'register_taxonomy'             => [ 'label' => 'Register Taxonomy' ],
			'unregister_taxonomy'           => [ 'label' => 'Unregister Taxonomy' ],
			'get_taxonomies'                => [ 'label' => 'Get Taxonomy (All)' ],
			'get_taxonomy'                  => [ 'label' => 'Get Taxonomy (Single)' ],
			'add_taxonomy_to_post'          => [ 'label' => 'Add Taxonomy to Post' ],
			'remove_taxonomy_from_post'     => [ 'label' => 'Remove Taxonomy from Post' ],
			'bulk_assign_terms_to_posts'    => [ 'label' => 'Bulk Assign Terms to Posts' ],
			'bulk_remove_terms_from_posts'  => [ 'label' => 'Bulk Remove Terms from Posts' ],
			'create_category'               => [ 'label' => 'Create Category' ],
			'update_category'               => [ 'label' => 'Update Category' ],
			'delete_category'               => [ 'label' => 'Delete Category' ],
			'add_category_to_post'          => [ 'label' => 'Add Category to Post' ],
			'get_categories'                => [ 'label' => 'Get Category (All)' ],
			'get_category'                  => [ 'label' => 'Get Category (Single)' ],
			'create_post_tag'               => [ 'label' => 'Create Post Tag' ],
			'update_post_tag'               => [ 'label' => 'Update Post Tag' ],
			'delete_post_tag'               => [ 'label' => 'Delete Post Tag' ],
			'add_tags_to_post'              => [ 'label' => 'Add Tags to Post' ],
			'remove_tags_from_post'         => [ 'label' => 'Remove Tags from Post' ],
			'get_post_tags'                 => [ 'label' => 'Get Post Tag (All)' ],
			'get_post_tag'                  => [ 'label' => 'Get Post Tag (Single)' ],
			'create_site'                   => [ 'label' => 'Create New Site' ],
			'delete_site'                   => [ 'label' => 'Delete Site' ],
			'add_user_to_site'              => [ 'label' => 'Add User to Site' ],
			'remove_user_from_site'         => [ 'label' => 'Remove User from Site' ],
			'untrash_post'                  => [ 'label' => 'Untrash Post' ],
			'untrash_comment'               => [ 'label' => 'Untrash Comment' ],
			'update_comment_count'          => [ 'label' => 'Update Comment Count' ],
			'set_comment_status'            => [ 'label' => 'Set Comment Status' ],
			'get_post_comments_all'         => [ 'label' => 'Get Post Comments (All)' ],
			'get_post_comments_single'      => [ 'label' => 'Get Post Comments (Single Post)' ],
			'get_user_comments'             => [ 'label' => 'Get User Comments' ],
			'get_user_comments_email'       => [ 'label' => 'Get User Comments (By Email)' ],
			'get_comment_metadata_all'      => [ 'label' => 'Get Comment Metadata (All)' ],
			'get_comment_metadata_single'   => [ 'label' => 'Get Comment Metadata (Single)' ],
			'create_comment'                => [ 'label' => 'Create New Comment' ],
			'reply_comment'                 => [ 'label' => 'Reply To Comment' ],
			'delete_comment'                => [ 'label' => 'Delete Comment' ],
			'add_plugin_theme_option'       => [ 'label' => 'Add Option' ],
			'update_option_advanced'        => [ 'label' => 'Update Option' ],
			'delete_option'                 => [ 'label' => 'Delete Option' ],
			'generate_attachment_metadata'  => [ 'label' => 'Generate Attachment Metadata' ],
			'regenerate_image_sizes'        => [ 'label' => 'Resize / Regenerate Image Sizes' ],
			'set_featured_image'            => [ 'label' => 'Set Media Featured Image' ],
			'update_title'                  => [ 'label' => 'Update Title' ],
			'update_post'                   => [ 'label' => 'Update Post' ],
			'update_status'                 => [ 'label' => 'Update Status' ],
			'duplicate_post'                => [ 'label' => 'Duplicate Post' ],
			'schedule_post'                 => [ 'label' => 'Schedule Post' ],
			'unschedule_post'               => [ 'label' => 'Unschedule Post' ],
			'update_post_feature_image'     => [ 'label' => 'Update Post Featured Image' ],
			'change_post_author'            => [ 'label' => 'Change Post Author' ],
			'trash_post'                    => [ 'label' => 'Trash Post / Page' ],
			'restore_post'                  => [ 'label' => 'Restore Post / Page from Trash' ],
			'delete_trash_post'             => [ 'label' => 'Delete Trash Post / Page' ],
			'delete_post'                   => [ 'label' => 'Delete Post / Page' ],
			'get_posts_all'                 => [ 'label' => 'Get Post (All)' ],
			'get_post_single'               => [ 'label' => 'Get Post (Single)' ],
			'get_posts_by_post_type'        => [ 'label' => 'Get Posts by Post Type' ],
			'get_posts_by_metadata'         => [ 'label' => 'Get Posts by Metadata' ],
			'get_posts_metadata_all'        => [ 'label' => 'Get Post Metadata (All)' ],
			'get_post_metadata_single'      => [ 'label' => 'Get Post Metadata (Single)' ],
			'get_post_permalink'            => [ 'label' => 'Get Post Permalink' ],
			'get_post_content'              => [ 'label' => 'Get Post Content' ],
			'get_post_excerpt'              => [ 'label' => 'Get Post Excerpt' ],
			'get_post_status'               => [ 'label' => 'Get Post Status' ],
			'get_post_type_all'             => [ 'label' => 'Get Post Type (All)' ],
			'get_post_type_single'          => [ 'label' => 'Get Post Type (Single Post)' ],
			'register_post_type'            => [ 'label' => 'Register Post Type' ],
			'unregister_post_type'          => [ 'label' => 'Unregister Post Type' ],
			'add_post_type_support'         => [ 'label' => 'Add Post Type Features' ],
			'approve_comment'               => [ 'label' => 'Approve Comment' ],
			'unapproved_comment'            => [ 'label' => 'Unapproved Comment' ],
			'mark_comment_spam'             => [ 'label' => 'Mark Comment as Spam' ],
			'unmark_comment_spam'           => [ 'label' => 'Unmark Comment as Spam' ],
			'trash_comment'                 => [ 'label' => 'Trash Comment' ],
			'restore_comment'               => [ 'label' => 'Restore Comment from Trash' ],
			'delete_trash_comment'          => [ 'label' => 'Delete Trash Comment' ],
			'activate_plugin'               => [ 'label' => 'Activate Plugin' ],
			'deactivate_plugin'             => [ 'label' => 'Deactivate Plugin' ],
			'switch_theme'                  => [ 'label' => 'Theme Switch' ],
			'add_media_image'               => [ 'label' => 'Add New Image' ],
			'delete_media'                  => [ 'label' => 'Delete Media' ],
			'rename_media'                  => [ 'label' => 'Rename Media' ],
			'get_media_all'                 => [ 'label' => 'Get Media (All)' ],
			'get_media_by_title'            => [ 'label' => 'Get Media (By Title)' ],
			'get_media_by_id'               => [ 'label' => 'Get Media (By ID)' ],
		];
	}

	private static function field_post_id(): array {
		return [
			[
				'key' => 'post_id',
				'label' => 'ID',
				'type' => 'expression',
				'required' => true
			]
		];
	}

	private static function field_post_title(): array {
		return [
			[
				'key' => 'post_title',
				'label' => 'New Title',
				'type' => 'expression',
				'required' => true
			]
		];
	}

	private static function field_post_type(): array {
		return [
			[
				'key' => 'post_type',
				'label' => 'Post Type',
				'type' => 'select',
				'dynamic' => [
					'integration' => 'wordpress',
					'query'       => 'post_types',
					'select'      => [ 'name', 'label' ],
				],
				'required' => true
			],
		];
	}

	private static function field_post_status(): array {
		return [
			[
				'key' => 'post_status',
				'label' => 'Status',
				'type' => 'select',
				'required' => true,
				'options' => [
					[
						'label' => 'Publish',
						'value' => 'publish'
					],
					[
						'label' => 'Pending',
						'value' => 'pending'
					],
					[
						'label' => 'Private',
						'value' => 'private'
					],
					[
						'label' => 'Draft',
						'value' => 'draft'
					],
				],
			],
		];
	}

	private static function field_comment_id(): array {
		return [
			[
				'key' => 'comment_id',
				'label' => 'Comment ID',
				'type' => 'expression',
				'required' => true
			]
		];
	}

	private static function field_user_id(): array {
		return [
			[
				'key' => 'user_id',
				'label' => 'User ID',
				'type' => 'select',
				'dynamic' => [
					'integration' => 'wordpress',
					'query'       => 'users',
					'select'      => [ 'ID', 'name' ],
				],
				'required' => true
			]
		];
	}

	private static function field_role( bool $required = true ): array {
		return [
			[
				'key' => 'role',
				'label' => 'Role',
				'type' => 'select',
				'dynamic' => [
					'integration' => 'wordpress',
					'query'       => 'roles',
					'select'      => [ 'name', 'label' ],
				],
				'required' => $required,
			],
		];
	}

	private static function field_taxonomy( bool $required = true ): array {
		return [
			[
				'key' => 'taxonomy',
				'label' => 'Taxonomy',
				'type' => 'select',
				'dynamic' => [
					'integration' => 'wordpress',
					'query'       => 'taxonomies',
					'select'      => [ 'name', 'label' ],
				],
				'required' => $required,
			],
		];
	}

	private static function field_term_id( string $taxonomy = '', bool $required = true ): array {
		$dynamic = [
			'integration' => 'wordpress',
			'query'       => 'terms',
			'select'      => [ 'term_id', 'name' ],
		];
		if ( '' !== $taxonomy ) {
			$dynamic['where'] = [
				'taxonomy' => $taxonomy,
			];
		}

		return [
			[
				'key' => 'term_id',
				'label' => 'Term ID',
				'type' => 'select',
				'dynamic' => $dynamic,
				'required' => $required,
			],
		];
	}

	private static function field_category_id( bool $required = true ): array {
		return [
			[
				'key' => 'category_id',
				'label' => 'Category',
				'type' => 'select',
				'dynamic' => [
					'integration' => 'wordpress',
					'query'       => 'categories',
					'select'      => [ 'id', 'label' ],
				],
				'required' => $required,
			],
		];
	}

	private static function field_attachment_id(): array {
		return [
			[
				'key' => 'attachment_id',
				'label' => 'Attachment ID',
				'type' => 'expression',
				'required' => true
			]
		];
	}

	private static function field_media_id(): array {
		return [
			[
				'key' => 'media_id',
				'label' => 'Media ID',
				'type' => 'expression',
				'required' => true
			]
		];
	}

	private static function field_media_title(): array {
		return [
			[
				'key' => 'title',
				'label' => 'Media Title',
				'type' => 'text',
				'required' => true
			]
		];
	}

	private static function field_user_email(): array {
		return [
			[
				'key' => 'user_email',
				'label' => 'User Email',
				'type' => 'email',
				'required' => true
			]
		];
	}



	public static function get_action_config_schema( string $action ): array {

		$schemas = [

			'untrash_post'           => self::field_post_id(),
			'update_comment_count'   => self::field_post_id(),
			'unschedule_post'        => self::field_post_id(),
			'get_post_single'        => self::field_post_id(),
			'get_posts_metadata_all' => self::field_post_id(),
			'get_post_permalink'     => self::field_post_id(),
			'get_post_content'       => self::field_post_id(),
			'get_post_excerpt'       => self::field_post_id(),
			'get_post_status'        => self::field_post_id(),
			'get_post_type_single'   => self::field_post_id(),
			'get_posts_by_post_type' => self::field_post_type(),
			'get_posts_by_metadata'  => self::field_post_type(),
			'unregister_post_type'   => self::field_post_type(),
			'update_status'          => [
				...self::field_post_id(),
				...self::field_post_type(),
				...self::field_post_status(),
			],

			'update_title'           => [
				...self::field_post_id(),
				...self::field_post_title(),
				...self::field_post_type(),
			],

			'duplicate_post' => [
				...self::field_post_id(),
				...self::field_post_title(),
				...self::field_post_type(),
			],

			'trash_post'             => [
				...self::field_post_id(),
				...self::field_post_type(),
			],
			'restore_post'           => [
				...self::field_post_id(),
				...self::field_post_type(),
			],
			'delete_trash_post'      => [
				...self::field_post_id(),
				...self::field_post_type(),
			],
			'delete_post'            => [
				...self::field_post_id(),
				...self::field_post_type(),
			],

			'create_post' => [
				[
					'key'      => 'post_title',
					'label'    => 'Post Title',
					'type'     => 'text',
					'required' => true,
				],
				[
					'key'         => 'post_type',
					'label'       => 'Post Type',
					'type'        => 'select',
					'required'    => true,
					'placeholder' => 'any post type',
					'dynamic'     => [
						'integration' => 'wordpress',
						'query'       => 'post_types',
						'select'      => [ 'name', 'label' ],
					],
				],
				[
					'key'         => 'post_status',
					'label'       => 'Post Status',
					'type'        => 'select',
					'required'    => true,
					'placeholder' => 'any post status',
					'options'     => [
						[
							'label' => 'Draft',
							'value' => 'draft'
						],
						[
							'label' => 'Publish',
							'value' => 'publish'
						],
						[
							'label' => 'Pending',
							'value' => 'pending'
						],
						[
							'label' => 'Private',
							'value' => 'private'
						],
						[
							'label' => 'Future',
							'value' => 'future'
						],
					],
				],
				[
					'key'         => 'post_author',
					'label'       => 'Post Author',
					'type'        => 'select',
					'placeholder' => 'any post author',
					'dynamic'     => [
						'integration' => 'wordpress',
						'query'       => 'users',
						'select'      => [ 'name', 'label' ],
					],
				],
				[
					'key'         => 'post_category',
					'label'       => 'Post Category',
					'type'        => 'select',
					'placeholder' => 'any post category',
					'dynamic'     => [
						'integration' => 'wordpress',
						'query'       => 'categories',
						'select'      => [ 'name', 'label' ],
					],
				],
				[
					'key'      => 'post_tags',
					'label'    => 'Select Post Tags',
					'type'     => 'multi-select',
					'required' => false,
					'dynamic'  => [
						'integration' => 'wordpress',
						'query'       => 'tags',
						'select'      => [ 'name', 'label' ],
					],
				],
				[
					'key'   => 'post_content',
					'label' => 'Post Content',
					'type'  => 'textarea',
				],
				[
					'key'   => 'post_excerpt',
					'label' => 'Post Excerpt',
					'type'  => 'textarea',
				],
				[
					'key'   => 'post_date',
					'label' => 'Post Date',
					'type'  => 'date',
				],
				[
					'key'   => 'post_date_gmt',
					'label' => 'Post Date GMT',
					'type'  => 'time',
				],
				[
					'key'   => 'post_name',
					'label' => 'Post Slug',
					'type'  => 'text',
				],
				[
					'key'         => 'post_parent',
					'label'       => 'Post Parent Id',
					'type'        => 'expression',
					'description' => 'Provide the Id of the parent post if this is a child post.',
				],
				[
					'key'         => 'post_password',
					'label'       => 'Post Password',
					'type'        => 'password',
					'description' => 'Only visible to those who know the password.',
				],
				[
					'key'    => 'featured_image_url',
					'label'  => 'Post Featured Image URL',
					'type'   => 'url',
					'toggle' => 'use_featured_image_id',
				],
				[
					'key'         => 'use_featured_image_id',
					'label'       => 'Use Featured Image ID instead of URL',
					'type'        => 'toggle',
					'description' => 'Enable to use Featured Image ID field instead of URL.',
				],
				[
					'key'         => 'taxonomy',
					'label'       => 'Select Taxonomy',
					'type'        => 'select',
					'placeholder' => 'any post Taxonomy',
					'dynamic'     => [
						'integration' => 'wordpress',
						'query'       => 'taxonomies',
						'select'      => [ 'name', 'label' ],
					],
				],
				[
					'key'         => 'taxonomy_term',
					'label'       => 'Select Taxonomy Term',
					'type'        => 'select',
					'placeholder' => 'any post Taxonomy Term',
					'dynamic'     => [
						'integration' => 'wordpress',
						'query'       => 'taxonomy_terms',
						'select'      => [ 'name', 'label' ],
						'depends_on'  => [ 'taxonomy' ],
					],
				],
				[
					'key'    => 'custom_fields',
					'label'  => 'Post Custom Field Map',
					'type'   => 'map',
					'fields' => [
						[
							'key' => 'key',
							'label' => 'Key',
							'type' => 'expression',
							'required' => true
						],
						[
							'key' => 'value',
							'label' => 'Value',
							'type' => 'expression',
							'required' => true
						],
					],
				],
			],

			'update_post' => [
				...self::field_post_id(),
				[
					'key'      => 'post_title',
					'label'    => 'Post Title',
					'type'     => 'expression',
					'required' => true,
				],
				[
					'key'         => 'post_type',
					'label'       => 'Post Type',
					'type'        => 'select',
					'required'    => true,
					'placeholder' => 'any post type',
					'dynamic'     => [
						'integration' => 'wordpress',
						'query'       => 'post_types',
						'select'      => [ 'name', 'label' ],
					],
				],
				[
					'key'         => 'post_status',
					'label'       => 'Post Status',
					'type'        => 'select',
					'required'    => true,
					'placeholder' => 'any post status',
					'options'     => [
						[
							'label' => 'Draft',
							'value' => 'draft'
						],
						[
							'label' => 'Publish',
							'value' => 'publish'
						],
						[
							'label' => 'Pending',
							'value' => 'pending'
						],
						[
							'label' => 'Private',
							'value' => 'private'
						],
						[
							'label' => 'Future',
							'value' => 'future'
						],
					],
				],
				[
					'key'         => 'post_author',
					'label'       => 'Post Author',
					'type'        => 'select',
					'placeholder' => 'any post author',
					'dynamic'     => [
						'integration' => 'wordpress',
						'query'       => 'users',
						'select'      => [ 'name', 'label' ],
					],
				],
				[
					'key'         => 'post_category',
					'label'       => 'Post Category',
					'type'        => 'select',
					'placeholder' => 'any post category',
					'dynamic'     => [
						'integration' => 'wordpress',
						'query'       => 'categories',
						'select'      => [ 'name', 'label' ],
					],
				],
				[
					'key'      => 'post_tags',
					'label'    => 'Select Post Tags',
					'type'     => 'multi-select',
					'required' => false,
					'dynamic'  => [
						'integration' => 'wordpress',
						'query'       => 'tags',
						'select'      => [ 'name', 'label' ],
					],
				],
				[
					'key'   => 'post_content',
					'label' => 'Post Content',
					'type'  => 'textarea',
				],
				[
					'key'   => 'post_excerpt',
					'label' => 'Post Excerpt',
					'type'  => 'textarea',
				],
				[
					'key'   => 'post_date',
					'label' => 'Post Date',
					'type'  => 'expression',
				],
				[
					'key'   => 'post_date_gmt',
					'label' => 'Post Date GMT',
					'type'  => 'expression',
				],
				[
					'key'   => 'post_name',
					'label' => 'Post Slug',
					'type'  => 'expression',
				],
				[
					'key'         => 'post_parent',
					'label'       => 'Post Parent Id',
					'type'        => 'expression',
					'description' => 'Provide the Id of the parent post if this is a child post.',
				],
				[
					'key'         => 'post_password',
					'label'       => 'Post Password',
					'type'        => 'expression',
					'description' => 'Only visible to those who know the password.',
				],
				[
					'key'    => 'featured_image_url',
					'label'  => 'Post Featured Image URL',
					'type'   => 'expression',
					'toggle' => 'use_featured_image_id',
				],
				[
					'key'         => 'use_featured_image_id',
					'label'       => 'Use Featured Image ID instead of URL',
					'type'        => 'toggle',
					'description' => 'Enable to use Featured Image ID field instead of URL.',
				],
				[
					'key'         => 'taxonomy',
					'label'       => 'Select Taxonomy',
					'type'        => 'select',
					'placeholder' => 'any post Taxonomy',
					'dynamic'     => [
						'integration' => 'wordpress',
						'query'       => 'taxonomies',
						'select'      => [ 'name', 'label' ],
					],
				],
				[
					'key'         => 'taxonomy_term',
					'label'       => 'Select Taxonomy Term',
					'type'        => 'select',
					'placeholder' => 'any post Taxonomy Term',
					'dynamic'     => [
						'integration' => 'wordpress',
						'query'       => 'taxonomy_terms',
						'select'      => [ 'name', 'label' ],
						'depends_on'  => [ 'taxonomy' ],
					],
				],
				[
					'key'    => 'custom_fields',
					'label'  => 'Post Custom Field Map',
					'type'   => 'map',
					'fields' => [
						[
							'key' => 'key',
							'label' => 'Key',
							'type' => 'expression',
							'required' => true
						],
						[
							'key' => 'value',
							'label' => 'Value',
							'type' => 'expression',
							'required' => true
						],
					],
				],
			],

			'schedule_post' => [
				...self::field_post_id(),
				[
					'key' => 'schedule_date',
					'label' => 'Schedule Date & Time',
					'type' => 'datetime',
					'required' => true
				],
				[
					'key' => 'post_status',
					'label' => 'Status',
					'type' => 'select',
					'options' => [
						[
							'label' => 'Future',
							'value' => 'future'
						],
						[
							'label' => 'Draft',
							'value' => 'draft'
						],
					],
					'default' => 'future',
				],
			],

			'update_post_feature_image' => [
				...self::field_post_id(),
				[
					'key' => 'image_id',
					'label' => 'Featured Image ID',
					'type' => 'expression',
					'required' => true
				],
			],

			'change_post_author' => [
				...self::field_post_id(),
				[
					'key' => 'author_id',
					'label' => 'Author ID',
					'type' => 'expression',
					'required' => true
				],
			],

			'get_post_metadata_single' => [
				...self::field_post_id(),
				[
					'key' => 'meta_kry',
					'label' => 'Post Meta Key',
					'type' => 'expression',
					'required' => true
				],
			],

			'get_post_comments_single' => self::field_post_id(),

			'get_user_comments_email' => self::field_user_email(),

			'register_post_type' => [
				[
					'key' => 'slug',
					'label' => 'Post Type Slug',
					'type' => 'text',
					'required' => true
				],
				[
					'key' => 'label',
					'label' => 'Label',
					'type' => 'text',
					'required' => false,
					'default' => ''
				],
				[
					'key' => 'hierarchical',
					'label' => 'Hierarchy (Category Style)',
					'type' => 'boolean',
					'required' => false
				],
				[
					'key' => 'public',
					'label' => 'Public',
					'type' => 'boolean',
					'required' => false,
					'default' => true
				],
				[
					'key' => 'show_in_rest',
					'label' => 'Show in REST API',
					'type' => 'boolean',
					'required' => false,
					'default' => true
				],
				[
					'key' => 'show_ui',
					'label' => 'Show UI',
					'type' => 'boolean',
					'required' => false,
					'default' => true
				],
				[
					'key' => 'show_in_menu',
					'label' => 'Show In Menu',
					'type' => 'boolean',
					'required' => false,
					'default' => true
				],
				[
					'key' => 'show_in_nav_menus',
					'label' => 'Show In Nav Menus',
					'type' => 'boolean',
					'required' => false,
					'default' => true
				],
				[
					'key' => 'show_in_admin_bar',
					'label' => 'Show In Admin Bar',
					'type' => 'boolean',
					'required' => false,
					'default' => true
				],
				[
					'key' => 'menu_icon',
					'label' => 'Menu Icon',
					'type' => 'text',
					'required' => false
				],
				[
					'key' => 'menu_position',
					'label' => 'Menu Position',
					'type' => 'number',
					'required' => false
				],
				[
					'key' => 'supports',
					'label' => 'Supports',
					'type' => 'multiselect',
					'options' => [
						[
							'label' => 'Title',
							'value' => 'title'
						],
						[
							'label' => 'Editor',
							'value' => 'editor'
						],
						[
							'label' => 'Thumbnail',
							'value' => 'thumbnail'
						],
						[
							'label' => 'Excerpt',
							'value' => 'excerpt'
						],
						[
							'label' => 'Comments',
							'value' => 'comments'
						],
						[
							'label' => 'Revisions',
							'value' => 'revisions'
						],
						[
							'label' => 'Author',
							'value' => 'author'
						],
						[
							'label' => 'Custom-Fields',
							'value' => 'custom-fields'
						],
					],
					'default' => [ 'title', 'editor' ],
					'required' => false,
				],
				[
					'key' => 'capability_type',
					'label' => 'Capability Type',
					'type' => 'text',
					'required' => false,
					'default' => 'post'
				],
				[
					'key' => 'description',
					'label' => 'Description',
					'type' => 'textarea',
					'required' => false,
					'default' => ''
				],
				[
					'key' => 'rewrite_slug',
					'label' => 'Custom URL Slug',
					'type' => 'textarea',
					'required' => false,
					'default' => ''
				],
			],

			'add_post_type_support' => [
				[
					'key' => 'post_type',
					'label' => 'Post Type',
					'type' => 'select',
					'dynamic' => [
						'integration' => 'wordpress',
						'query'       => 'post_types',
						'select'      => [ 'name', 'label' ],
					],
					'required' => true,
				],
				[
					'key' => 'features',
					'label' => 'Features (Supports)',
					'type' => 'multiselect',
					'options' => [
						[
							'label' => 'Title',
							'value' => 'title'
						],
						[
							'label' => 'Editor',
							'value' => 'editor'
						],
						[
							'label' => 'Thumbnail',
							'value' => 'thumbnail'
						],
						[
							'label' => 'Excerpt',
							'value' => 'excerpt'
						],
						[
							'label' => 'Comments',
							'value' => 'comments'
						],
						[
							'label' => 'Revisions',
							'value' => 'revisions'
						],
						[
							'label' => 'Author',
							'value' => 'author'
						],
						[
							'label' => 'Custom-Fields',
							'value' => 'custom-fields'
						],
					],
					'required' => false,
				],
			],

			'untrash_comment'             => self::field_comment_id(),
			'delete_comment'              => self::field_comment_id(),
			'approve_comment'             => self::field_comment_id(),
			'unapproved_comment'          => self::field_comment_id(),
			'mark_comment_spam'           => self::field_comment_id(),
			'unmark_comment_spam'         => self::field_comment_id(),
			'trash_comment'               => self::field_comment_id(),
			'restore_comment'             => self::field_comment_id(),
			'delete_trash_comment'        => self::field_comment_id(),
			'get_comment_metadata_all'    => self::field_comment_id(),
			'get_comment_metadata_single' => [
				...self::field_comment_id(),
				[
					'key' => 'meta_key',
					'label' => 'Comment Meta Key',
					'type' => 'expression',
					'required' => true
				],
			],

			'set_comment_status' => [
				...self::field_comment_id(),
				[
					'key' => 'status',
					'label' => 'Status',
					'type' => 'select',
					'required' => true,
					'options' => [
						[
							'label' => 'Approved',
							'value' => '1'
						],
						[
							'label' => 'Pending',
							'value' => '0'
						],
						[
							'label' => 'Spam',
							'value' => 'spam'
						],
						[
							'label' => 'Trash',
							'value' => 'trash'
						],
					]
				],
			],

			'create_comment' => [
				...self::field_post_id(),
				[
					'key' => 'author_name',
					'label' => 'Author Name',
					'type' => 'expression',
					'required' => true
				],
				[
					'key' => 'author_email',
					'label' => 'Author Email',
					'type' => 'email',
					'required' => true
				],
				[
					'key' => 'content',
					'label' => 'Comment Content',
					'type' => 'textarea',
					'required' => true
				],
			],

			'reply_comment' => [
				[
					'key' => 'parent_id',
					'label' => 'Parent Comment ID',
					'type' => 'expression',
					'required' => true
				],
				[
					'key' => 'author_name',
					'label' => 'Author Name',
					'type' => 'expression',
					'required' => true
				],
				[
					'key' => 'author_email',
					'label' => 'Author Email',
					'type' => 'email',
					'required' => true
				],
				[
					'key' => 'content',
					'label' => 'Reply Content',
					'type' => 'textarea',
					'required' => true
				],
			],

			'create_user' => [
				[
					'key' => 'user_login',
					'label' => 'Username',
					'type' => 'text',
					'required' => true
				],
				[
					'key' => 'user_email',
					'label' => 'Email',
					'type' => 'email',
					'required' => true
				],
				[
					'key' => 'user_pass',
					'label' => 'Password',
					'type' => 'text'
				],
				[
					'key' => 'display_name',
					'label' => 'Display Name',
					'type' => 'text'
				],
				[
					'key' => 'first_name',
					'label' => 'First Name',
					'type' => 'text'
				],
				[
					'key' => 'last_name',
					'label' => 'Last Name',
					'type' => 'text'
				],
				[
					'key' => 'role',
					'label' => 'Role',
					'type' => 'select',
					'dynamic' => [
						'integration' => 'wordpress',
						'query'       => 'roles',
						'select'      => [ 'name', 'label' ],
					]
				],
			],

			'update_user' => [
				...self::field_user_id(),
				[
					'key' => 'user_email',
					'label' => 'Email',
					'type' => 'email',
					'required' => true
				],
				[
					'key' => 'user_pass',
					'label' => 'Password',
					'type' => 'text'
				],
				[
					'key' => 'display_name',
					'label' => 'Display Name',
					'type' => 'text'
				],
				[
					'key' => 'first_name',
					'label' => 'First Name',
					'type' => 'text'
				],
				[
					'key' => 'last_name',
					'label' => 'Last Name',
					'type' => 'text'
				],
				[
					'key' => 'role',
					'label' => 'Role',
					'type' => 'select',
					'dynamic' => [
						'integration' => 'wordpress',
						'query'       => 'roles',
						'select'      => [ 'name', 'label' ],
					]
				],
			],

			'delete_user' => [
				...self::field_user_id(),
				[
					'key' => 'reassign',
					'label' => 'Reassign User ID',
					'type' => 'select',
					'dynamic' => [
						'integration' => 'wordpress',
						'query'       => 'users',
						'select'      => [ 'ID', 'name' ],
					]
				],
			],

			'activate_user'          => self::field_user_id(),
			'deactivate_user'        => self::field_user_id(),
			'get_users_by_role'      => self::field_role(),
			'get_user_by_id'         => self::field_user_id(),
			'get_user_by_email'      => [
				[
					'key' => 'email',
					'label' => 'User Email',
					'type' => 'email',
					'required' => true,
				],
			],
			'get_user_by_field'      => [
				[
					'key' => 'field',
					'label' => 'Field',
					'type' => 'select',
					'options' => [
						[
							'label' => 'User ID',
							'value' => 'id'
						],
						[
							'label' => 'Email',
							'value' => 'email'
						],
						[
							'label' => 'Login',
							'value' => 'login'
						],
						[
							'label' => 'Slug',
							'value' => 'slug'
						],
					],
					'default' => 'id',
					'required' => true,
				],
				[
					'key' => 'value',
					'label' => 'Field Value',
					'type' => 'text',
					'required' => true,
				],
			],
			'get_user_meta_all'      => self::field_user_id(),
			'get_user_meta_single'   => [
				...self::field_user_id(),
				[
					'key' => 'meta_key',
					'label' => 'Meta Key',
					'type' => 'text',
					'required' => true,
				],
			],
			'update_user_meta'       => [
				...self::field_user_id(),
				[
					'key' => 'meta_key',
					'label' => 'Meta Key',
					'type' => 'text',
					'required' => true,
				],
				[
					'key' => 'meta_value',
					'label' => 'Meta Value',
					'type' => 'textarea',
				],
			],
			'send_password_reset_email' => [
				[
					'key' => 'user_login_or_email',
					'label' => 'Username or Email',
					'type' => 'email',
					'required' => true,
				],
			],

			'create_role' => [
				[
					'key' => 'role',
					'label' => 'Role Slug',
					'type' => 'text',
					'required' => true,
				],
				[
					'key' => 'display_name',
					'label' => 'Display Name',
					'type' => 'text',
					'required' => true,
				],
				[
					'key' => 'capabilities',
					'label' => 'Capabilities (CSV)',
					'type' => 'textarea',
				],
			],
			'delete_role'            => self::field_role(),
			'add_user_role'          => [ ...self::field_user_id(), ...self::field_role() ],
			'remove_user_role'       => [ ...self::field_user_id(), ...self::field_role() ],
			'update_user_role'       => [ ...self::field_user_id(), ...self::field_role() ],
			'get_roles' => [
				[
					'key' => 'search',
					'label' => 'Search',
					'type' => 'text',
				],
			],
			'get_caps' => [
				[
					'key' => 'search',
					'label' => 'Search',
					'type' => 'text',
				],
			],
			'get_role_caps'          => self::field_role(),
			'add_role_caps'          => [
				...self::field_role(),
				[
					'key' => 'caps',
					'label' => 'Capabilities (CSV)',
					'type' => 'textarea',
					'required' => true,
				],
			],
			'remove_role_caps'       => [
				...self::field_role(),
				[
					'key' => 'caps',
					'label' => 'Capabilities (CSV)',
					'type' => 'textarea',
					'required' => true,
				],
			],
			'get_user_caps'          => self::field_user_id(),
			'add_user_caps'          => [
				...self::field_user_id(),
				[
					'key' => 'caps',
					'label' => 'Capabilities (CSV)',
					'type' => 'textarea',
					'required' => true,
				],
			],
			'remove_user_caps'       => [
				...self::field_user_id(),
				[
					'key' => 'caps',
					'label' => 'Capabilities (CSV)',
					'type' => 'textarea',
					'required' => true,
				],
			],

			'get_term'               => [ ...self::field_taxonomy(), ...self::field_term_id() ],
			'get_terms_by_taxonomy'  => [
				...self::field_taxonomy(),
				[
					'key' => 'hide_empty',
					'label' => 'Hide Empty Terms',
					'type' => 'boolean',
				],
				[
					'key' => 'search',
					'label' => 'Search',
					'type' => 'text',
				],
				[
					'key' => 'limit',
					'label' => 'Limit',
					'type' => 'number',
					'required' => true,
				],
			],
			'get_term_by_field'      => [
				...self::field_taxonomy(),
				[
					'key' => 'field',
					'label' => 'Field',
					'type' => 'select',
					'options' => [
						[
							'label' => 'ID',
							'value' => 'id'
						],
						[
							'label' => 'Slug',
							'value' => 'slug'
						],
						[
							'label' => 'Name',
							'value' => 'name'
						],
					],
					'default' => 'id',
					'required' => true,
				],
				[
					'key' => 'value',
					'label' => 'Field Value',
					'type' => 'text',
					'required' => true,
				],
			],
			'create_term'            => [
				...self::field_taxonomy(),
				[
					'key' => 'name',
					'label' => 'Name',
					'type' => 'text',
					'required' => true,
				],
				[
					'key' => 'slug',
					'label' => 'Slug',
					'type' => 'text',
				],
				[
					'key' => 'parent',
					'label' => 'Parent Term ID',
					'type' => 'select',
					'dynamic' => [
						'integration' => 'wordpress',
						'query'       => 'terms',
						'select'      => [ 'term_id', 'name' ],
						'where'       => [ 'taxonomy' => 'category' ],
					],
				],
				[
					'key' => 'description',
					'label' => 'Description',
					'type' => 'textarea',
				],
			],
			'update_term'            => [
				...self::field_taxonomy(),
				...self::field_term_id(),
				[
					'key' => 'name',
					'label' => 'Name',
					'type' => 'text',
				],
				[
					'key' => 'slug',
					'label' => 'Slug',
					'type' => 'text',
				],
				[
					'key' => 'parent',
					'label' => 'Parent Term ID',
					'type' => 'select',
					'dynamic' => [
						'integration' => 'wordpress',
						'query'       => 'terms',
						'select'      => [ 'term_id', 'name' ],
						'where'       => [ 'taxonomy' => 'category' ],
					],
				],
				[
					'key' => 'description',
					'label' => 'Description',
					'type' => 'textarea',
				],
			],
			'delete_term'            => [ ...self::field_taxonomy(), ...self::field_term_id() ],
			'register_taxonomy'      => [
				[
					'key' => 'taxonomy',
					'label' => 'Taxonomy Slug',
					'type' => 'text',
					'required' => true,
				],
				[
					'key' => 'object_type',
					'label' => 'Object Types (CSV)',
					'type' => 'textarea',
					'required' => true,
				],
				[
					'key' => 'args',
					'label' => 'Arguments (JSON)',
					'type' => 'textarea',
				],
			],
			'unregister_taxonomy'    => self::field_taxonomy(),
			'get_taxonomies'         => [
				[
					'key' => 'search',
					'label' => 'Search',
					'type' => 'text',
				],
			],
			'get_taxonomy'           => self::field_taxonomy(),
			'add_taxonomy_to_post'   => [
				...self::field_post_id(),
				...self::field_taxonomy(),
				[
					'key' => 'terms',
					'label' => 'Terms (IDs/Slugs, CSV)',
					'type' => 'textarea',
					'required' => true,
				],
				[
					'key' => 'append',
					'label' => 'Append',
					'type' => 'boolean',
				],
			],
			'remove_taxonomy_from_post' => [
				...self::field_post_id(),
				...self::field_taxonomy(),
				[
					'key' => 'terms',
					'label' => 'Terms (IDs/Slugs, CSV)',
					'type' => 'textarea',
					'required' => true,
				],
			],
			'bulk_assign_terms_to_posts' => [
				...self::field_taxonomy(),
				[
					'key' => 'post_ids',
					'label' => 'Post IDs (CSV)',
					'type' => 'textarea',
					'required' => true,
				],
				[
					'key' => 'terms',
					'label' => 'Terms (IDs/Slugs, CSV)',
					'type' => 'textarea',
					'required' => true,
				],
				[
					'key' => 'append',
					'label' => 'Append',
					'type' => 'boolean',
				],
			],
			'bulk_remove_terms_from_posts' => [
				...self::field_taxonomy(),
				[
					'key' => 'post_ids',
					'label' => 'Post IDs (CSV)',
					'type' => 'textarea',
					'required' => true,
				],
				[
					'key' => 'terms',
					'label' => 'Terms (IDs/Slugs, CSV)',
					'type' => 'textarea',
					'required' => true,
				],
			],

			'create_category'        => [
				[
					'key' => 'name',
					'label' => 'Category Name',
					'type' => 'text',
					'required' => true,
				],
				[
					'key' => 'slug',
					'label' => 'Slug',
					'type' => 'text',
				],
				[
					'key' => 'parent',
					'label' => 'Parent Category',
					'type' => 'select',
					'dynamic' => [
						'integration' => 'wordpress',
						'query'       => 'categories',
						'select'      => [ 'id', 'label' ],
					],
				],
				[
					'key' => 'description',
					'label' => 'Description',
					'type' => 'textarea',
				],
			],
			'update_category'        => [
				...self::field_category_id(),
				[
					'key' => 'name',
					'label' => 'Category Name',
					'type' => 'text',
				],
				[
					'key' => 'slug',
					'label' => 'Slug',
					'type' => 'text',
				],
				[
					'key' => 'parent',
					'label' => 'Parent Category',
					'type' => 'select',
					'dynamic' => [
						'integration' => 'wordpress',
						'query'       => 'categories',
						'select'      => [ 'id', 'label' ],
					],
				],
				[
					'key' => 'description',
					'label' => 'Description',
					'type' => 'textarea',
				],
			],
			'delete_category'        => self::field_category_id(),
			'add_category_to_post'   => [
				...self::field_post_id(),
				[
					'key' => 'categories',
					'label' => 'Category IDs (CSV)',
					'type' => 'textarea',
					'required' => true,
				],
				[
					'key' => 'append',
					'label' => 'Append',
					'type' => 'boolean',
				],
			],
			'get_categories'         => [
				[
					'key' => 'search',
					'label' => 'Search',
					'type' => 'text',
				],
				[
					'key' => 'hide_empty',
					'label' => 'Hide Empty',
					'type' => 'boolean',
				],
				[
					'key' => 'limit',
					'label' => 'Limit',
					'type' => 'number',
					'required' => true,
				],
			],
			'get_category'           => self::field_category_id(),

			'add_plugin_theme_option' => [
				[
					'key' => 'option_name',
					'label' => 'Option',
					'type' => 'text',
					'required' => true
				],
				[
					'key' => 'value',
					'label' => 'Value',
					'type' => 'expression'
				],
			],

			'update_option_advanced' => [
				[
					'key' => 'option_name',
					'label' => 'Option Name',
					'type' => 'text',
					'required' => true
				],
				[
					'key' => 'value',
					'label' => 'New Value',
					'type' => 'expression'
				],
			],

			'delete_option' => [
				[
					'key' => 'option_name',
					'label' => 'Option',
					'type' => 'text',
					'required' => true
				],
			],

			'generate_attachment_metadata' => self::field_attachment_id(),
			'regenerate_image_sizes'       => self::field_attachment_id(),
			'get_media_by_title'           => self::field_media_title(),
			'get_media_by_id'              => self::field_media_id(),
			'set_featured_image' => [
				...self::field_post_id(),
				...self::field_attachment_id(),
			],

			'add_media_image' => [
				[
					'key' => 'image_url',
					'label' => 'Image URL',
					'type' => 'text',
					'required' => true
				],
				[
					'key' => 'image_title',
					'label' => 'Image Title',
					'type' => 'text',
					'required' => false
				],
				[
					'key' => 'alternative_text',
					'label' => 'Alternative Text',
					'type' => 'text',
					'required' => false
				],
				[
					'key' => 'caption',
					'label' => 'Caption',
					'type' => 'textarea',
					'required' => false
				],
				[
					'key' => 'description',
					'label' => 'Description',
					'type' => 'textarea',
					'required' => false
				],
			],

			'delete_media' => [
				...self::field_media_id(),
				[
					'key' => 'force_delete',
					'label' => 'Force Delete',
					'type' => 'boolean',
					'default' => false,
					'required' => false
				],
			],

			'rename_media' => [
				...self::field_media_id(),
				[
					'key' => 'new_title',
					'label' => 'New Title',
					'type' => 'expression',
					'required' => false
				],
			],

			'activate_plugin' => [
				[
					'key' => 'plugin',
					'label' => 'Inactive Plugin',
					'type' => 'select',
					'dynamic' => [
						'integration' => 'wordpress',
						'query'       => 'inactive_plugins',
						'select'      => [ 'file', 'name' ],
					],
					'required' => true,
				],
			],

			'deactivate_plugin' => [
				[
					'key' => 'plugin',
					'label' => 'Active Plugin',
					'type' => 'select',
					'dynamic' => [
						'integration' => 'wordpress',
						'query'       => 'active_plugins',
						'select'      => [ 'file', 'name' ],
					],
					'required' => true,
				],
			],

			'switch_theme' => [
				[
					'key' => 'theme',
					'label' => 'Theme Switch',
					'type' => 'select',
					'dynamic' => [
						'integration' => 'wordpress',
						'query'       => 'deactivate_theme',
						'select'      => [ 'file', 'name' ],
					],
					'required' => true,
				],
			],

			'authenticate_user' => [
				[
					'key' => 'user_login',
					'label' => 'Username or Email',
					'type' => 'email',
					'required' => true
				],
				[
					'key' => 'user_password',
					'label' => 'Password',
					'type' => 'text',
					'required' => true
				],
				[
					'key' => 'remember',
					'label' => 'Remember',
					'type' => 'boolean'
				],
				[
					'key' => 'secure_cookie',
					'label' => 'Secure Cookie',
					'type' => 'boolean'
				],
			],

			'logout_user' => [
				[
					'key' => 'force',
					'label' => 'Force',
					'type' => 'boolean',
				],
			],

		];

		return $schemas[ $action ] ?? [];
	}

	public static function execute_node( array $node, array $input ): array {

		$config = $node['data']['config'] ?? [];
		$event = $node['data']['event'] ?? '';

		$method = 'action_' . $event;

		if ( method_exists( static::class, $method ) ) {
			return static::$method( $config, $input );
		}

		return [
			'port' => 'main',
			'data' => $input
		];
	}

	public static function get_dynamic_queries(): array {
		return [
			'post_types' => [ self::class, 'query_post_types' ],
			'posts'      => [ self::class, 'query_posts' ],
			'terms'      => [ self::class, 'query_terms' ],
			'terms_with_any' => [ self::class, 'query_terms_with_any' ],
			'users'      => [ self::class, 'query_users' ],
			'users_with_any' => [ self::class, 'query_users_with_any' ],
			'taxonomies' => [ self::class, 'query_taxonomies' ],
			'categories' => [ self::class, 'query_categories' ],
			'tags'       => [ self::class, 'query_tags' ],
			'taxonomy_terms' => [ self::class, 'query_taxonomy_terms' ],
			'roles'      => [ self::class, 'query_roles' ],
			'caps'       => [ self::class, 'query_caps' ],
			'active_plugins'    => [ self::class, 'query_active_plugins' ],
			'inactive_plugins'  => [ self::class, 'query_deactivate_plugins' ],
			'deactivate_theme'  => [ self::class, 'query_deactivate_theme' ],
		];
	}
}
