<?php
// @generated by build-free-packages.mjs — DO NOT EDIT.
/**
 * WordPress Trigger Definitions
 *
 * Configuration-driven definitions for WordPress core triggers.
 * Returns plain arrays instead of TriggerDefinition objects, allowing
 * PackageLoader to create the objects during initialization.
 *
 * @package Sequensy\Integrations\WordPress
 * @since 2.0.0
 */

declare(strict_types=1);

namespace Sequensy\Integrations\WordPress\Triggers;

use Sequensy\Integrations\WordPress\Helpers\UserFields;

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}

/**
 * Class WordPressTriggers
 *
 * Provides configuration-driven definitions for WordPress core triggers.
 */
final class WordPressTriggers {

	/**
	 * Get all trigger definitions.
	 *
	 * @return array<array> Array of trigger definition arrays.
	 */
	public static function get_definitions(): array {
		return array(
			self::post_published(),
			self::user_registration(),
			self::user_profile_update(),
			self::comment_submitted(),
		);
	}

	/**
	 * Post Published trigger definition.
	 *
	 * @return array
	 */
	public static function post_published(): array {
		return array(
			'type'             => 'post_published',
			'label'            => __( 'Post Published', 'sequensy' ),
			'description'      => __( 'Triggers when a post is published', 'sequensy' ),
			'hook'             => 'publish_post',
			'priority'         => 10,
			'accepted_args'    => 2,
			'available_fields' => array(
				array(
					'name'        => 'post_id',
					'type'        => 'integer',
					'description' => __( 'Unique identifier for the post', 'sequensy' ),
					'example'     => '456',
				),
				array(
					'name'        => 'post_title',
					'type'        => 'string',
					'description' => __( 'Title of the post', 'sequensy' ),
					'example'     => 'My Blog Post',
				),
				array(
					'name'        => 'post_content',
					'type'        => 'string',
					'description' => __( 'Content of the post', 'sequensy' ),
					'example'     => 'Post content here...',
				),
				array(
					'name'        => 'post_excerpt',
					'type'        => 'string',
					'description' => __( 'Excerpt of the post', 'sequensy' ),
					'example'     => 'A brief summary...',
				),
				array(
					'name'        => 'post_author',
					'type'        => 'integer',
					'description' => __( 'User ID of the post author', 'sequensy' ),
					'example'     => '1',
				),
				array(
					'name'        => 'author_email',
					'type'        => 'string',
					'description' => __( 'Email of the post author', 'sequensy' ),
					'example'     => 'author@example.com',
				),
				array(
					'name'        => 'post_url',
					'type'        => 'string',
					'description' => __( 'URL of the post', 'sequensy' ),
					'example'     => 'https://example.com/my-post',
				),
				array(
					'name'        => 'post_date',
					'type'        => 'datetime',
					'description' => __( 'Date when the post was published', 'sequensy' ),
					'example'     => '2024-01-15 10:30:00',
				),
			),
			'build_context'    => function ( int $post_id, $post ): ?array {
				if ( ! $post || 'publish' !== $post->post_status ) {
					return null;
				}

				$author = get_userdata( $post->post_author );

				return array(
					'post_id'      => $post_id,
					'post_title'   => $post->post_title,
					'post_content' => $post->post_content,
					'post_excerpt' => $post->post_excerpt,
					'post_author'  => $post->post_author,
					'author_email' => $author ? $author->user_email : '',
					'post_url'     => get_permalink( $post_id ),
					'post_date'    => $post->post_date,
					'post_type'    => $post->post_type,
				);
			},
		);
	}

	/**
	 * User Registration trigger definition.
	 *
	 * @return array
	 */
	public static function user_registration(): array {
		return array(
			'type'             => 'user_registration',
			'label'            => __( 'User Registration', 'sequensy' ),
			'description'      => __( 'Triggers when a new user registers on the WordPress site', 'sequensy' ),
			'hook'             => 'user_register',
			'priority'         => 10,
			'accepted_args'    => 1,
			'available_fields' => UserFields::available_fields(),
			'build_context'    => function ( int $user_id ): ?array {
				return UserFields::build_context( $user_id );
			},
		);
	}

	/**
	 * User Profile Update trigger definition.
	 *
	 * @return array
	 */
	public static function user_profile_update(): array {
		return array(
			'type'             => 'user_profile_update',
			'label'            => __( 'User Profile Updated', 'sequensy' ),
			'description'      => __( 'Triggers when a user profile is updated', 'sequensy' ),
			'hook'             => 'profile_update',
			'priority'         => 10,
			'accepted_args'    => 3,
			'available_fields' => UserFields::available_fields(),
			'build_context'    => function ( int $user_id, $old_user_data, $userdata ): ?array {
				return UserFields::build_context( $user_id );
			},
		);
	}

	/**
	 * Comment Submitted trigger definition.
	 *
	 * @return array
	 */
	public static function comment_submitted(): array {
		return array(
			'type'             => 'comment_submitted',
			'label'            => __( 'Comment Submitted', 'sequensy' ),
			'description'      => __( 'Triggers when a new comment is submitted', 'sequensy' ),
			'hook'             => 'comment_post',
			'priority'         => 10,
			'accepted_args'    => 3,
			'available_fields' => array(
				array(
					'name'        => 'comment_id',
					'type'        => 'integer',
					'description' => __( 'Unique identifier for the comment', 'sequensy' ),
					'example'     => '789',
				),
				array(
					'name'        => 'comment_content',
					'type'        => 'string',
					'description' => __( 'Content of the comment', 'sequensy' ),
					'example'     => 'Great post!',
				),
				array(
					'name'        => 'comment_author',
					'type'        => 'string',
					'description' => __( 'Name of the comment author', 'sequensy' ),
					'example'     => 'John Doe',
				),
				array(
					'name'        => 'comment_author_email',
					'type'        => 'string',
					'description' => __( 'Email of the comment author', 'sequensy' ),
					'example'     => 'commenter@example.com',
				),
				array(
					'name'        => 'post_id',
					'type'        => 'integer',
					'description' => __( 'ID of the post being commented on', 'sequensy' ),
					'example'     => '456',
				),
				array(
					'name'        => 'post_title',
					'type'        => 'string',
					'description' => __( 'Title of the post being commented on', 'sequensy' ),
					'example'     => 'My Blog Post',
				),
				array(
					'name'        => 'comment_approved',
					'type'        => 'string',
					'description' => __( 'Approval status of the comment', 'sequensy' ),
					'example'     => '1',
				),
			),
			'build_context'    => function ( int $comment_id, $approved, $comment_data ): ?array {
				$comment = get_comment( $comment_id );
				if ( ! $comment ) {
					return null;
				}

				$post = get_post( $comment->comment_post_ID );

				return array(
					'comment_id'           => $comment_id,
					'comment_content'      => $comment->comment_content,
					'comment_author'       => $comment->comment_author,
					'comment_author_email' => $comment->comment_author_email,
					'comment_author_url'   => $comment->comment_author_url,
					'post_id'              => $comment->comment_post_ID,
					'post_title'           => $post ? $post->post_title : '',
					'comment_approved'     => $approved,
					'comment_date'         => $comment->comment_date,
				);
			},
		);
	}
}
