<?php
/**
 * Per-customer automation journey events for admin timeline.
 *
 * @package CartCue
 */

namespace CartCue;

defined( 'ABSPATH' ) || exit;

final class JourneyRecorder {

	public static function record( string $email, string $event_type, string $detail, int $workflow_id = 0 ): void {
		$email = strtolower( trim( sanitize_email( $email ) ) );
		if ( ! is_email( $email ) ) {
			return;
		}

		global $wpdb;
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery
		$wpdb->insert(
			DB::journey_events(),
			[
				'customer_email' => $email,
				'workflow_id'    => max( 0, $workflow_id ),
				'event_type'     => sanitize_key( $event_type ),
				'detail'         => sanitize_text_field( $detail ),
				'created_at'     => current_time( 'mysql' ),
			],
			[ '%s', '%d', '%s', '%s', '%s' ]
		);
	}
}
