<?php
/**
 * AJAX handlers for row actions.
 *
 * @package 404Zen
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class LGBF_Ajax {

	/**
	 * Hooks.
	 */
	public static function init() {
		add_action( 'wp_ajax_lgbf_edit_url', array( __CLASS__, 'edit_url' ) );
		add_action( 'wp_ajax_lgbf_unlink', array( __CLASS__, 'unlink' ) );
		add_action( 'wp_ajax_lgbf_dismiss', array( __CLASS__, 'dismiss' ) );
		add_action( 'wp_ajax_lgbf_undismiss', array( __CLASS__, 'undismiss' ) );
		add_action( 'wp_ajax_lgbf_recheck', array( __CLASS__, 'recheck' ) );
		add_action( 'wp_ajax_lgbf_hide_task', array( __CLASS__, 'hide_task' ) );
		add_action( 'wp_ajax_lgbf_subscribe', array( __CLASS__, 'subscribe' ) );
	}

	/**
	 * Shared guard: capability + nonce; returns the link ID.
	 *
	 * @return int
	 */
	private static function guard() {
		if ( ! current_user_can( LGBF_Admin::CAPABILITY ) ) {
			wp_send_json_error( array( 'message' => __( 'You are not allowed to do that.', '404zen-broken-link-fixer' ) ), 403 );
		}
		check_ajax_referer( 'lgbf_admin_nonce', 'nonce' );

		$id = isset( $_POST['link_id'] ) ? absint( $_POST['link_id'] ) : 0;
		if ( ! $id ) {
			wp_send_json_error( array( 'message' => __( 'Missing link ID.', '404zen-broken-link-fixer' ) ), 400 );
		}

		return $id;
	}

	/**
	 * Sends a uniform response for fixer results.
	 *
	 * @param true|array|WP_Error $result Fixer result.
	 */
	private static function respond( $result ) {
		if ( is_wp_error( $result ) ) {
			wp_send_json_error( array( 'message' => $result->get_error_message() ), 400 );
		}

		wp_send_json_success( is_array( $result ) ? $result : array() );
	}

	public static function edit_url() {
		$id      = self::guard();
		$new_url = isset( $_POST['new_url'] ) ? esc_url_raw( wp_unslash( $_POST['new_url'] ) ) : '';

		self::respond( LGBF_Fixer::edit_url( $id, $new_url ) );
	}

	public static function unlink() {
		self::respond( LGBF_Fixer::unlink( self::guard() ) );
	}

	public static function dismiss() {
		self::respond( LGBF_Fixer::dismiss( self::guard() ) );
	}

	public static function undismiss() {
		self::respond( LGBF_Fixer::undismiss( self::guard() ) );
	}

	/**
	 * Explicit newsletter opt-in from the dashboard card.
	 */
	public static function subscribe() {
		if ( ! current_user_can( LGBF_Admin::CAPABILITY ) ) {
			wp_send_json_error( array( 'message' => __( 'You are not allowed to do that.', '404zen-broken-link-fixer' ) ), 403 );
		}
		check_ajax_referer( 'lgbf_admin_nonce', 'nonce' );

		$email  = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : '';
		$result = LGBF_Optin::record( $email, 'dashboard_card' );
		if ( is_wp_error( $result ) ) {
			wp_send_json_error( array( 'message' => $result->get_error_message() ), 400 );
		}

		wp_send_json_success();
	}

	/**
	 * Hides a setup task row from the issues card (per user).
	 */
	public static function hide_task() {
		if ( ! current_user_can( LGBF_Admin::CAPABILITY ) ) {
			wp_send_json_error( array( 'message' => __( 'You are not allowed to do that.', '404zen-broken-link-fixer' ) ), 403 );
		}
		check_ajax_referer( 'lgbf_admin_nonce', 'nonce' );

		$task = isset( $_POST['task'] ) ? sanitize_key( wp_unslash( $_POST['task'] ) ) : '';
		if ( ! in_array( $task, LGBF_Admin::$dismissible_tasks, true ) ) {
			wp_send_json_error( array( 'message' => __( 'Unknown task.', '404zen-broken-link-fixer' ) ), 400 );
		}

		$hidden = LGBF_Admin::hidden_tasks();
		if ( ! in_array( $task, $hidden, true ) ) {
			$hidden[] = $task;
			update_user_meta( get_current_user_id(), 'lgbf_hidden_tasks', $hidden );
		}

		wp_send_json_success();
	}

	public static function recheck() {
		$id     = self::guard();
		$result = LGBF_Fixer::recheck( $id );

		if ( is_wp_error( $result ) ) {
			self::respond( $result );
		}

		wp_send_json_success(
			array(
				'status'      => $result['status'],
				'http_status' => (int) $result['http_status'],
			)
		);
	}
}
