<?php

namespace NeurogenesisTheme\Core\Admin\Actions;

use NeurogenesisTheme\Helpers\RatingNotice;

defined('ABSPATH') || exit;

final class RatingNoticeAjax
{
    public function __construct()
    {
        add_action('wp_ajax_neurogenesis_theme_rating_notice', [$this, 'handle']);
    }

    public function handle(): void
    {
        if (!current_user_can('edit_posts')) {
            wp_send_json_error(['message' => __('Forbidden.', 'thinko')], 403);
        }

        check_ajax_referer('neurogenesis_theme_rating_notice', 'nonce');

        if (RatingNotice::is_debug()) {
            wp_send_json_success(['ok' => true, 'debug' => true]);
        }

        $action = isset($_POST['rating_action'])
            ? sanitize_key(wp_unslash((string) $_POST['rating_action']))
            : '';

        switch ($action) {
            case 'dismiss':
            case 'not_for_me':
            case 'already_reviewed':
            case 'rated_low':
            case 'rated_high':
            case 'need_help':
                RatingNotice::dismiss_permanently();
                break;
            case 'remind':
                RatingNotice::remind_later();
                break;
            case 'snooze':
                RatingNotice::record_snooze_without_rating();
                break;
            default:
                wp_send_json_error(['message' => __('Unknown action.', 'thinko')], 400);
        }

        wp_send_json_success(['ok' => true]);
    }
}
