<?php
namespace Uncanny_Automator\Integrations\Discord;

use Exception;

/**
 * Class DISCORD_ASSIGN_ROLE_TO_MEMBER
 *
 * @package Uncanny_Automator
 *
 * @property Discord_App_Helpers $helpers
 * @property Discord_Api_Caller $api
 */
class DISCORD_ASSIGN_ROLE_TO_MEMBER extends \Uncanny_Automator\Recipe\App_Action {

	/**
	 * Prefix for action code / meta.
	 *
	 * @var string
	 */
	public $prefix = 'DISCORD_ASSIGN_ROLE_TO_MEMBER';

	/**
	 * Server meta key.
	 *
	 * @var string
	 */
	private $server_key;

	/**
	 * Set up action.
	 *
	 * @return void
	 */
	public function setup_action() {
		// Set server key property.
		$this->server_key = $this->helpers->get_const( 'ACTION_SERVER_META_KEY' );

		$this->set_integration( 'DISCORD' );
		$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/discord/' ) );
		$this->set_requires_user( false );
		$this->set_sentence(
			sprintf(
				// translators: %1$s Role name, %2$s Member name
				esc_attr_x( 'Assign {{a role:%1$s}} to {{a member:%2$s}}', 'Discord', 'uncanny-automator' ),
				$this->get_action_meta(),
				'MEMBER:' . $this->get_action_meta()
			)
		);
		$this->set_readable_sentence( esc_attr_x( 'Assign {{a role}} to {{a member}}', 'Discord', 'uncanny-automator' ) );
		$this->set_action_tokens(
			array(
				'SERVER_ID'   => array(
					'name' => esc_html_x( 'Server ID', 'Discord', 'uncanny-automator' ),
					'type' => 'text',
				),
				'SERVER_NAME' => array(
					'name' => esc_html_x( 'Server name', 'Discord', 'uncanny-automator' ),
					'type' => 'text',
				),
				'ROLE_NAME'   => array(
					'name' => esc_html_x( 'Role name', 'Discord', 'uncanny-automator' ),
					'type' => 'text',
				),
				'USERNAME'    => array(
					'name' => esc_html_x( 'Username', 'Discord', 'uncanny-automator' ),
					'type' => 'text',
				),
			),
			$this->get_action_code()
		);
	}

	/**
	 * Define options
	 *
	 * @return array
	 */
	public function options() {
		return array(
			$this->helpers->get_server_select_config( $this->server_key ),
			$this->helpers->get_verified_members_select_config( 'MEMBER' ),
			$this->helpers->get_server_roles_select_config( $this->get_action_meta(), $this->server_key ),
		);
	}

	/**
	 * Process the action.
	 *
	 * @param int $user_id
	 * @param array $action_data
	 * @param int $recipe_id
	 * @param array $args
	 * @param array $parsed
	 *
	 * @return bool
	 * @throws Exception
	 */
	protected function process_action( $user_id, $action_data, $recipe_id, $args, $parsed ) {

		// Required fields - throws error if not set and valid.
		$server_id = $this->helpers->get_server_id_from_parsed( $parsed, $this->server_key );
		$member_id = $this->helpers->get_member_id_from_parsed( $parsed, 'MEMBER' );
		$role_id   = $this->helpers->get_role_id_from_parsed( $parsed, $this->get_action_meta() );

		// Prepare the body.
		$body = array(
			'action'    => 'assign_role_to_member',
			'member_id' => $member_id,
			'role_id'   => $role_id,
		);

		// Send the message.
		$response = $this->api->discord_request( $body, $action_data, $server_id );

		// Check for errors.
		$status_code = isset( $response['statusCode'] ) ? absint( $response['statusCode'] ) : 0;
		if ( 204 !== $status_code ) {
			throw new Exception( esc_html_x( 'Error adding role to user.', 'Discord', 'uncanny-automator' ) );
		}

		// Hydrate tokens.
		$this->hydrate_tokens(
			array(
				'SERVER_ID'   => $server_id,
				'SERVER_NAME' => $parsed[ $this->server_key . '_readable' ],
				'ROLE_NAME'   => $this->helpers->get_role_name_token_value(
					$parsed[ $this->get_action_meta() . '_readable' ],
					$role_id,
					$server_id
				),
				'USERNAME'    => $this->helpers->get_member_username_token_value( $member_id ),
			)
		);

		return true;
	}
}
