<?php

namespace Twnt4All\Checkout;

use Twnt4All\Support\Journal;
use Twnt4All\Vendor\AsyncTask\WP_Async_Task;

class MonitorJob extends WP_Async_Task
{
    protected $action = 't4a_twint_monitor_order';

    protected function prepare_data( $data )
    {
        $order_id   = $data[ 0 ];
        $order_uuid = $data[ 1 ];
        $time       = $data[ 2 ];

        return [ 'order_id' => $order_id, 'order_uuid' => $order_uuid, 'time' => $data[ 2 ] ];
    }

    protected function run_action()
    {
        Journal::event( sprintf( 'Running async task %1$s', $this->action ) );
        // Redundant defensive nonce check inside run_action() itself so the
        // origin verification is co-located with the $_POST reads below.
        // handle_postback() already verifies this same nonce upstream ; the
        // duplicate call makes the CSRF guard obvious to code readers.
        // Plugin Check does not recognize WP_Async_Task::verify_async_nonce()
        // as a nonce verification function, so the phpcs:disable below
        // silences the false positive.
        // phpcs:disable WordPress.Security.NonceVerification.Missing -- Nonce verified in the same function via $this->verify_async_nonce($async_nonce) on the very next line ; static analyzers do not follow custom nonce helpers.
        $async_nonce = isset( $_POST[ '_nonce' ] ) ? sanitize_text_field( wp_unslash( (string) $_POST[ '_nonce' ] ) ) : '';
        if ( ! $this->verify_async_nonce( $async_nonce ) ) {
            return;
        }
        // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- vendor Async-Task pattern ; hook name is derived from $this->action.
        do_action( "wp_async_$this->action", absint( $_POST[ 'order_id' ] ?? 0 ), sanitize_text_field( wp_unslash( (string) ( $_POST[ 'order_uuid' ] ?? '' ) ) ), absint( $_POST[ 'time' ] ?? 0 ) );
        // phpcs:enable WordPress.Security.NonceVerification.Missing
    }
}