<?php

/**
 * Class responsible for adding various hooks and actions to the plugin.
 */

namespace BoostsiteAIContentGenerator;

/**
 * Hooks and action class
 */
class HooksAndActions {
	/**
	 * The application domain.
	 *
	 * @var string
	 */
	private $prefix;

	/**
	 * Initialize this class.
	 *
	 * @param string $prefix Plugin prefix.
	 */
	public function __construct( $prefix ) {
		$this->prefix = $prefix;
		$this->add_actions();
	}

	/**
	 * Add actions.
	 *
	 * @return void
	 */
	public function add_actions() {
		add_action( 'transition_post_status', array( $this, 'update_post_status' ), 10, 3 );

		// Register AJAX action for API key reset
		add_action( 'wp_ajax_boostsite_ai_handle_api_key_reset', array( $this, 'boostsite_ai_handle_api_key_reset' ) );
		add_action( 'wp_ajax_nopriv_boostsite_ai_handle_api_key_reset', array( $this, 'boostsite_ai_handle_api_key_reset' ) );
	}

	/**
	 * Update post status.
	 *
	 * @param string $new_status New post status.
	 * @param string $old_status Old post status.
	 * @param object $post       Post object.
	 *
	 * @return void
	 */
	public function update_post_status( $new_status, $old_status, $post ) {
		// Check if the statuses are correct and post has boostsite meta key.
		if ( 'publish' === $new_status && 'future' === $old_status && 'post' === $post->post_type && get_post_meta( $post->ID, 'wp_autopost_ai_post_status', true ) ) {
			// Update the post meta key.
			update_post_meta( $post->ID, 'wp_autopost_ai_post_status', 'published' );
		}
	}

	/**
	 * Handle API key reset AJAX action.
	 *
	 * @return void
	 */
	public function boostsite_ai_handle_api_key_reset() {
		// Check if user is logged in.
		if ( ! is_user_logged_in() ) {
			echo esc_html__( 'You must be logged in to reset the API key.', 'boostsite-ai-content-generator' );
			exit;
		}

		// Check if user has permission to reset the API key.
		if ( ! current_user_can( 'manage_options' ) ) {
			echo esc_html__( 'You do not have permission to reset the API key.', 'boostsite-ai-content-generator' );
			exit;
		}

		// Check if the token is present.
		$token = sanitize_text_field( $_GET['token'] ?? '' );

		// Check if the token is valid.
		if ( empty( $token ) ) {
			echo esc_html__( 'Token is required.', 'boostsite-ai-content-generator' );
			exit;
		}

		// Reset the API key.
		try {
			$settings_controller = new \BoostsiteAIContentGenerator\Api\SettingController();

			// Create a mock request object with the token
			$request = new \WP_REST_Request();
			$request->set_param( 'token', $token );

			$result = $settings_controller->reset_api_key( $request );

			// Check if the reset was successful
			if ( is_wp_error( $result ) ) {
				$error_message = $result->get_error_message();
				echo esc_html( $error_message );
				exit;
			}

			$response_data = $result->get_data();
			if ( ! isset( $response_data['success'] ) || ! $response_data['success'] ) {
				echo esc_html__( 'Failed to reset API key. Please try again.', 'boostsite-ai-content-generator' );
				exit;
			}

			// Redirect with a message.
			$settings_url = admin_url( 'admin.php?page=' . $this->prefix . '#/settings' );
			$redirect_url = add_query_arg(
				array(
					'notification_message' => urlencode( esc_html__( 'API key has been successfully reset!', 'boostsite-ai-content-generator' ) ),
					'notification_type'    => 'success',
				),
				$settings_url
			);

			wp_redirect( $redirect_url );
			exit;
		} catch ( \Exception $e ) {
			echo esc_html__( 'Failed to reset API key: ', 'boostsite-ai-content-generator' ) . esc_html( $e->getMessage() );
			exit;
		}
	}
}
