<?php

namespace CPBW\Ajax;

class Notices {

    public function __construct() {
        add_action( 'wp_ajax_cpbw_dismiss_promotional_notice', [ $this, 'dismiss_promotional_notice' ] );
		add_action( 'wp_ajax_cpbw_dismiss_review_notice', [ $this, 'dismiss_review_notice' ] );
	}

	/**
	 * Handle the AJAX request to dismiss the review notice.
	 */
	public function dismiss_review_notice() {
		check_ajax_referer( 'cpbw_dismiss_notice', 'nonce' );

		if ( ! is_user_logged_in() ) {
			wp_send_json_error( [ 'message' => 'Unauthorized request' ], 403 );
		}

		$dismiss_type = sanitize_text_field( $_POST['dismiss_type'] );

		if( $dismiss_type === 'later' ) {
			// Snooze for 7 days
			$dismiss_type = time() + ( 7 * DAY_IN_SECONDS );
		}
		
		if ( $dismiss_type === 'never' ) {
			$dismiss_type = 'never';
		}

		update_user_meta( get_current_user_id(), 'cpbw_review_dismissed', $dismiss_type );

		wp_send_json_success( [ 'message' => 'Notice dismissed' ] );
	}

    /**
	 * Handle the AJAX request to dismiss the promotional notice.
	 */
	public function dismiss_promotional_notice() {
		check_ajax_referer( 'cpbw_dismiss_notice', 'nonce' );

		if ( ! is_user_logged_in() ) {
			wp_send_json_error( [ 'message' => 'Unauthorized request' ], 403 );
		}

		update_user_meta( get_current_user_id(), 'cpbw_promotional_notice_november_2025', 'never' );

		wp_send_json_success( [ 'message' => 'Notice dismissed' ] );
	}

}