<?php
/**
 * Automation Admin Init
 *
 * @package momoacg
 */
class MoMo_ACG_Automation_Admin {
	/**
	 * Constructor
	 */
	public function __construct() {
		add_action( 'momo_add_submenu_to_momoacg', array( $this, 'momo_add_submenu_of_automation' ), 16 );
		add_action( 'admin_enqueue_scripts', array( $this, 'momoacg_automation_print_admin_ss' ) );

		add_action( 'wp_ajax_momoacg_automation_addedit', array( $this, 'momoacg_automation_addedit' ) );
		add_action( 'wp_ajax_momoacg_automation_load_workflows_list', array( $this, 'momoacg_automation_load_workflows_list' ) );
		add_action( 'wp_ajax_momoacg_automation_toggle_status', array( $this, 'momoacg_automation_toggle_status' ) );
		add_action( 'wp_ajax_momoacg_automation_edit_form', array( $this, 'momoacg_automation_edit_form' ) );
		add_action( 'wp_ajax_momoacg_automation_delete_automation', array( $this, 'momoacg_automation_delete_automation' ) );
	}
	/**
	 * AJAX handler for loading the automation edit form
	 *
	 * @return void
	 */
	public function momoacg_automation_delete_automation() {
		global $momoacg;
		$res = check_ajax_referer( 'momoacg_security_key', 'security' );
		if ( isset( $_POST['action'] ) && 'momoacg_automation_delete_automation' !== $_POST['action'] ) {
			return;
		}
		$workflow_id = isset( $_POST['workflow_id'] ) ? sanitize_text_field( wp_unslash( $_POST['workflow_id'] ) ) : '';
		wp_delete_post( $workflow_id );
		$response = array(
			'status'  => 'good',
			'message' => esc_html__( 'Workflow deleted successfully', 'momoacg' ),
		);
		echo wp_json_encode( $response );
		exit;
	}
	/**
	 * AJAX handler for loading the automation edit form
	 *
	 * @return void
	 */
	public function momoacg_automation_edit_form() {
		global $momoacg;
		$res = check_ajax_referer( 'momoacg_security_key', 'security' );
		if ( isset( $_POST['action'] ) && 'momoacg_automation_edit_form' !== $_POST['action'] ) {
			return;
		}
		$workflow_id = isset( $_POST['workflow_id'] ) ? sanitize_text_field( wp_unslash( $_POST['workflow_id'] ) ) : '';
		$type        = isset( $_POST['type'] ) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : 'new';
		$meta_data   = get_post_meta( $workflow_id, '', true );
		ob_start();
		$post_id = $workflow_id;
		include_once $momoacg->plugin_path . 'automation/admin/pages/momoacg-automation-form.php';
		$contents = ob_get_clean();
		$response = array(
			'status'  => 'good',
			'content' => $contents,
			'title'   => esc_html__( 'Edit', 'momoacg' ),
			'message' => esc_html__( 'Edit form generated successfully', 'momoacg' ),
		);
		echo wp_json_encode( $response );
		exit;
	}
	/**
	 * Change Workflow Status
	 *
	 * @return void
	 */
	public function momoacg_automation_toggle_status() {
		$res = check_ajax_referer( 'momoacg_security_key', 'security' );
		if ( isset( $_POST['action'] ) && 'momoacg_automation_toggle_status' !== $_POST['action'] ) {
			return;
		}
		$workflow_id = isset( $_POST['workflow_id'] ) ? sanitize_text_field( wp_unslash( $_POST['workflow_id'] ) ) : '';
		$status      = isset( $_POST['status'] ) ? sanitize_text_field( wp_unslash( $_POST['status'] ) ) : 'on';
		if ( empty( $workflow_id ) ) {
			return;
		}
		if ( 'off' === $status ) {
			$new_status = 'on';
			$btn        = esc_html__( 'On', 'momoacg' );
		} else {
			$new_status = 'off';
			$btn        = esc_html__( 'Off', 'momoacg' );
		}
		update_post_meta( $workflow_id, 'workflow_status', $new_status );
		$response = array(
			'status'     => 'good',
			'new_status' => $new_status,
			'text'       => $btn,
		);
		echo wp_json_encode( $response );
		exit;
	}
	/**
	 * Add/Edit automation
	 *
	 * @since 1.2.6
	 *
	 * @return void
	 */
	public function momoacg_automation_addedit() {
		$res = check_ajax_referer( 'momoacg_security_key', 'security' );
		if ( isset( $_POST['action'] ) && 'momoacg_automation_addedit' !== $_POST['action'] ) {
			return;
		}
		$meta_data  = array();
		$post_title = isset( $_POST['post_title'] ) ? sanitize_text_field( wp_unslash( $_POST['post_title'] ) ) : '';
		$content    = isset( $_POST['content'] ) ? wpautop( wp_unslash( $_POST['content'] ) ) : '';

		$meta_data['to']             = isset( $_POST['to'] ) ? sanitize_text_field( wp_unslash( $_POST['to'] ) ) : '';
		$meta_data['from']           = isset( $_POST['from'] ) ? sanitize_text_field( wp_unslash( $_POST['from'] ) ) : '';
		$meta_data['reply_to']       = isset( $_POST['reply_to'] ) ? sanitize_text_field( wp_unslash( $_POST['reply_to'] ) ) : '';
		$meta_data['reply_to_name']  = isset( $_POST['reply_to_name'] ) ? sanitize_text_field( wp_unslash( $_POST['reply_to_name'] ) ) : '';
		$meta_data['reply_to_email'] = isset( $_POST['reply_to_email'] ) ? sanitize_text_field( wp_unslash( $_POST['reply_to_email'] ) ) : '';
		$meta_data['subject']        = isset( $_POST['subject'] ) ? sanitize_text_field( wp_unslash( $_POST['subject'] ) ) : '';
		$meta_data['heading']        = isset( $_POST['heading'] ) ? sanitize_text_field( wp_unslash( $_POST['heading'] ) ) : '';
		$meta_data['preheader']      = isset( $_POST['preheader'] ) ? sanitize_text_field( wp_unslash( $_POST['preheader'] ) ) : '';
		$meta_data['event']          = isset( $_POST['event'] ) ? sanitize_text_field( wp_unslash( $_POST['event'] ) ) : '';
		$meta_data['event_action']   = isset( $_POST['event_action'] ) ? sanitize_text_field( wp_unslash( $_POST['event_action'] ) ) : '';
		$type                        = isset( $_POST['type'] ) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : 'new';
		$post_id                     = isset( $_POST['workflow_id'] ) ? sanitize_text_field( wp_unslash( $_POST['workflow_id'] ) ) : 0;

		$is_new = 0 === (int) $post_id;

		$post_data = array(
			'post_title'   => $post_title,
			'post_type'    => 'momoacg_automation',
			'post_status'  => 'publish',
			'post_content' => $content,
		);

		if ( $is_new ) {
			$post_id                      = wp_insert_post( $post_data );
			$meta_data['workflow_status'] = 'off';
		} else {
			$post_data['ID'] = $post_id;
			wp_update_post( $post_data );
		}
		if ( $post_id ) {
			foreach ( $meta_data as $key => $value ) {
				update_post_meta( $post_id, $key, $value );
			}
		}
		$response = array(
			'status'  => 'good',
			'message' => $is_new ? 'Automation created successfully' : 'Automation updated successfully',
		);
		echo wp_json_encode( $response );
		exit;
	}
	/**
	 * AJAX handler for loading the automation workflows list
	 *
	 * @since 1.2.6
	 *
	 * @return void
	 */
	public function momoacg_automation_load_workflows_list() {
		$res = check_ajax_referer( 'momoacg_security_key', 'security' );
		if ( isset( $_POST['action'] ) && 'momoacg_automation_load_workflows_list' !== $_POST['action'] ) {
			return;
		}
		$args = array(
			'post_type'      => 'momoacg_automation',
			'posts_per_page' => -1,
			'post_status'    => 'publish',
		);
		$query = new WP_Query( $args );

		if ( $query->have_posts() ) {
			ob_start();
			?>
			<table class="wp-list-table widefat fixed striped momo-acg-automation-list">
				<thead>
					<tr>
						<th><?php esc_html_e( 'Title', 'momoacg' ); ?></th>
						<th><?php esc_html_e( 'Published Date', 'momoacg' ); ?></th>
						<th><?php esc_html_e( 'Status', 'momoacg' ); ?></th>
						<th><?php esc_html_e( 'Action', 'momoacg' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php
					while ( $query->have_posts() ) :
						$query->the_post();
						$post_id = get_the_ID();
						$status  = get_post_meta( $post_id, 'workflow_status', true );
						if ( empty( $status ) ) {
							$status = 'off';
						}
						?>
						<tr>
							<td><?php the_title(); ?></td>
							<td><?php echo get_the_date( 'Y-m-d' ); ?></td>
							<td style="text-align: center;">
								<button class="momo-workflow-toggle-status" data-id="<?php echo esc_attr( $post_id ); ?>" data-status="<?php echo esc_attr( $status ); ?>">
									<?php echo ucfirst( $status ); ?>
								</button>
							</td>
							<td>
								<a href="#" class="edit-automation momoacg-automation-ed-btn" data-id="<?php echo esc_attr( $post_id ); ?>"><?php esc_html_e( 'Edit', 'momoacg' ); ?></a>
								<a href="#" class="delete-automation momoacg-automation-ed-btn" data-id="<?php echo esc_attr( $post_id ); ?>"><?php esc_html_e( 'Delete', 'momoacg' ); ?></a>
							</td>
						</tr>
						<?php
					endwhile;
					wp_reset_postdata();
					?>
				</tbody>
			</table>
			<?php
		} else {
			?>
			<p><?php esc_html_e( 'No workflows found.', 'momoacg' ); ?></p>
			<?php
		}
		$contents = ob_get_clean();
		$html     = wp_kses_post( $contents );
		$response = array(
			'status'  => 'good',
			'content' => $html,
		);
		echo wp_json_encode( $response );
		exit;
	}
	/**
	 * Adds Submenu
	 */
	public function momo_add_submenu_of_automation() {
		global $momoacg;
		add_submenu_page(
			'momoacg',
			esc_html__( 'AI Tools Automation', 'momoacg' ),
			'Automation',
			'manage_options',
			'momoacg-automation',
			array( $this, 'momoacg_automation_add_admin_settings_page' )
		);
	}
	/**
	 * Settings Page
	 */
	public function momoacg_automation_add_admin_settings_page() {
		global $momoacg;
		include_once $momoacg->plugin_path . 'automation/admin/pages/momo-acg-automation-settings.php';
	}
	/**
	 * Enqueue script and styles
	 */
	public function momoacg_automation_print_admin_ss() {
		$current_screen = get_current_screen();
		if ( isset( $current_screen->base ) && 'ai-tools_page_momoacg-automation' === $current_screen->base ) {
			global $momoacg;

			$momo_acg_insights_settings = get_option( 'momo_acg_insights_settings' );

			wp_enqueue_style( 'momoacg_automation_style', $momoacg->plugin_url . 'automation/assets/momoacg-automation.css', array(), $momoacg->version );
			wp_register_script( 'momoacg_automation_admin', $momoacg->plugin_url . 'automation/assets/momoacg-automation.js', array( 'jquery' ), $momoacg->version, true );
			wp_enqueue_script( 'momoacg_automation_admin' );
			$ajaxurl = array(
				'ajaxurl'            => admin_url( 'admin-ajax.php' ),
				'momoacg_ajax_nonce' => wp_create_nonce( 'momoacg_security_key' ),
				'creating_post'      => esc_html__( 'Adding / Updating Automation', 'momoacg' ),
				'empty_field'        => esc_html__( 'Empty required field(s)', 'momoacg' ),
				'generating'         => esc_html__( 'Generating Workflow', 'momoacg' ),
				'delete'             => esc_html__( 'Deleting Workflow', 'momoacg' ),
			);
			wp_localize_script( 'momoacg_automation_admin', 'momoacg_automation_admin', $ajaxurl );
		}
	}
}
new MoMo_ACG_Automation_Admin();
