<?php

namespace Gigneo\Modules;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

use Gigneo\Modules;

/**
 * Class Close_Account
 *
 * Plugin term-meta class for Gigneo.
 *
 * @since 0.0.1
 */
class Close_Account {


	/**
	 * The single instance of the class.
	 *
	 * @var Close_Account|null
	 * @since 0.0.1
	 */
	private static $instance = null;

	/**
	 * Ensures only one instance of the class is loaded or can be loaded.
	 *
	 * @return Close_Account
	 * @since 0.0.1
	 */
	public static function instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * Constructor for the class.
	 *
	 * @since 0.0.1
	 */
	public function __construct() {
		//Reopen Account
		add_action( 'wp_ajax_gigneo_account_reopen', array( $this, 'account_reopen' ) );

		//Action Button
		add_action( 'gigneo_post_type_actions', array( $this, 'post_type_actions' ), 99, 3 );
	}
	public function account_reopen() {

		// Check if nonce is valid
		$security = isset( $_POST['security'] ) ? sanitize_text_field( wp_unslash( $_POST['security'] ) ) : '';
		if ( empty( $security ) || ! wp_verify_nonce( $security, 'gigneo_security' ) ) {
			wp_send_json_error( array( 'message' => esc_html__( 'Security verification failed. Please reload and try again.', 'gigneo' ) ), 403 );
		}

		// Check if the user has permission to edit settings (usually admin)
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( array( 'message' => esc_html__( 'You do not have permission to do that.', 'gigneo' ) ), 403 );
		}

		$id        = isset( $_POST['id'] ) ? sanitize_text_field( wp_unslash( $_POST['id'] ) ) : '';
		$author_id = get_post_field( 'post_author', $id );

		if ( empty( $id ) || empty( $author_id ) ) {
			wp_send_json_error( array( 'message' => esc_html__( 'Something went wrong.', 'gigneo' ) ), 409 );
		}

		//Profile IDs
		$freelancer_profile_id = get_user_meta( $author_id, '_gigneo_um_freelancer_profile_id', true );
		$employer_profile_id   = get_user_meta( $author_id, '_gigneo_um_employer_profile_id', true );

		if ( ! empty( $freelancer_profile_id ) ) {
			$updated = wp_update_post(
				array(
					'ID'          => $freelancer_profile_id,
					'post_status' => 'publish',
				)
			);
			if ( is_wp_error( $updated ) ) {
				wp_send_json_error( array( 'message' => esc_html__( 'Failed to deactivate account. Please try again.', 'gigneo' ) ), 500 );
			}
		}

		if ( ! empty( $employer_profile_id ) ) {
			$updated = wp_update_post(
				array(
					'ID'          => $employer_profile_id,
					'post_status' => 'publish',
				)
			);
			if ( is_wp_error( $updated ) ) {
				wp_send_json_error( array( 'message' => esc_html__( 'Failed to deactivate account. Please try again.', 'gigneo' ) ), 500 );
			}
		}

		delete_user_meta( $author_id, '_gigneo_um_account_closed_info' );

		//Trigger Email
		if ( in_array( 'close_account', gigneo_active_modules(), true ) ) {
			$wc_mailer = WC()->mailer();
			$wc_emails = $wc_mailer->get_emails();
			$wc_emails['Gigneo_Account_Reactivated']->trigger( $author_id );
		}

		$response            = array();
		$response['message'] = esc_html__( 'Account reopen successfully.', 'gigneo' );

		wp_send_json_success( $response, 200 );
	}

	/**
	 * Adds a post type action button for closed accounts.
	 *
	 * @param int    $post_id    The post ID.
	 * @param int    $author_id  The author user ID.
	 * @param string $post_type  The post type.
	 *
	 * @since 0.0.1
	 */
	public function post_type_actions( $post_id, $author_id, $post_type ) {

		if ( 'gigneo_freelancer' !== $post_type && 'gigneo_employer' !== $post_type ) {
			return;
		}

		$post_status = get_post_status( $post_id );

		$is_blocked     = 'blocked' === $post_status;
		$closed_account = 'closed' === $post_status;

		if ( $is_blocked || ! $closed_account ) {
			return;
		}

		$close_icon = GIGNEO_DIR_URL . 'assets/img/icon/account-closed.png';
		?>
		<div class="gn-post-type-action gn-action-account-closed">
			<button type="button" data-gn-id="<?php echo esc_attr( $post_id ); ?>" class="gn-account-closed-btn"
				data-gn-tooltip="<?php echo esc_html__( 'Account Closed', 'gigneo' ); ?>">
				<img src="<?php echo esc_url( $close_icon ); ?>" alt="<?php echo esc_html__( 'Account Closed', 'gigneo' ); ?>">
			</button>
		</div>
		<?php
	}
}