<?php

namespace Uncanny_Automator\Integrations\Brevo;

/**
 * Class BREVO_REMOVE_CONTACT_FROM_LIST
 *
 * @package Uncanny_Automator
 *
 * @property Brevo_App_Helpers $helpers
 * @property Brevo_Api_Caller $api
 */
class BREVO_REMOVE_CONTACT_FROM_LIST extends \Uncanny_Automator\Recipe\App_Action {

	/**
	 * The action prefix.
	 *
	 * @var string
	 */
	public $prefix = 'REMOVE_CONTACT_FROM_LIST';

	/**
	 * Define and register the action by pushing it into the Automator object.
	 *
	 * @return void
	 */
	public function setup_action() {
		$this->set_integration( 'BREVO' );
		$this->set_action_code( $this->prefix . '_CODE' );
		$this->set_action_meta( $this->prefix . '_META' );
		$this->set_is_pro( false );
		$this->set_support_link( Automator()->get_author_support_link( $this->action_code, 'knowledge-base/brevo/' ) );
		$this->set_requires_user( false );
		$this->set_sentence(
			sprintf(
				// translators: %1$s Contact Email, %2$s List
				esc_attr_x( 'Remove {{a contact:%1$s}} from {{a list:%2$s}}', 'Brevo', 'uncanny-automator' ),
				$this->prefix . '_EMAIL:' . $this->get_action_meta(),
				$this->get_action_meta()
			)
		);
		$this->set_readable_sentence( esc_attr_x( 'Remove {{a contact}} from {{a list}}', 'Brevo', 'uncanny-automator' ) );
		$this->set_background_processing( true );
	}

	/**
	 * Define options.
	 *
	 * @return array
	 */
	public function options() {
		return array(
			array(
				'option_code' => $this->prefix . '_EMAIL',
				'label'       => esc_html_x( 'Email', 'Brevo', 'uncanny-automator' ),
				'input_type'  => 'email',
				'required'    => true,
			),
			array(
				'option_code'           => $this->get_action_meta(),
				'label'                 => esc_html_x( 'List', 'Brevo', 'uncanny-automator' ),
				'input_type'            => 'select',
				'required'              => true,
				'remote_data'           => $this->helpers->remote_data_load_config( 'lists' ),
				'supports_custom_value' => false,
			),
		);
	}

	/**
	 * Process the action.
	 *
	 * @param int $user_id
	 * @param array $action_data
	 * @param int $recipe_id
	 * @param array $args
	 * @param array $parsed
	 *
	 * @return bool
	 */
	protected function process_action( $user_id, $action_data, $recipe_id, $args, $parsed ) {
		$email    = $this->helpers->get_email_from_parsed( $parsed, $this->prefix . '_EMAIL' );
		$list_id  = $this->helpers->get_list_id_from_parsed( $parsed, $this->get_action_meta() );
		$response = $this->api->remove_contact_from_list( $email, $list_id, $action_data );
		return true;
	}
}
