<?php

namespace NeurogenesisTheme\Core\Admin\Actions;

use NeurogenesisTheme\Helpers\OnboardingNotice;

defined('ABSPATH') || exit;

final class OnboardingNoticeAjax
{
    public function __construct()
    {
        add_action('wp_ajax_neurogenesis_theme_onboarding_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_onboarding_notice', 'nonce');

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

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

        switch ($action) {
            case 'dismiss':
                OnboardingNotice::dismiss();
                break;
            case 'snooze':
                OnboardingNotice::snooze();
                break;
            default:
                wp_send_json_error(['message' => __('Unknown action.', 'thinko')], 400);
        }

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