<?php
/**
 * Form submission context.
 *
 * @package SubmissionInbox
 */

namespace SUBIN\Forms;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Holds the current lead id during a form submission request.
 */
class FormSubmissionContext {
	/**
	 * Current lead id.
	 *
	 * @var int|null
	 */
	private $current_lead_id = null;

	/**
	 * Current source.
	 *
	 * @var string
	 */
	private $current_source = '';

	/**
	 * Whether mail logging should be suppressed for the current request context.
	 *
	 * @var bool
	 */
	private $suppress_mail_logging = false;

	/**
	 * Set current lead id.
	 *
	 * @param int|null $lead_id Lead id.
	 * @return void
	 */
	public function set_current_lead_id( $lead_id ) {
		$lead_id = absint( $lead_id );
		$this->current_lead_id = $lead_id > 0 ? $lead_id : null;
	}

	/**
	 * Set current source.
	 *
	 * @param string $source Source.
	 * @return void
	 */
	public function set_current_source( $source ) {
		$source = sanitize_key( $source );
		$allowed = array( 'contact_form_7', 'submission_inbox_form', 'submission_inbox_auto_reply', 'wpforms', 'fluent_forms', 'forminator', 'ninja_forms' );

		$this->current_source = in_array( $source, $allowed, true ) ? $source : '';
	}

	/**
	 * Get current lead id.
	 *
	 * @return int|null
	 */
	public function get_current_lead_id() {
		return $this->current_lead_id;
	}

	/**
	 * Get current source.
	 *
	 * @return string
	 */
	public function get_current_source() {
		return $this->current_source;
	}

	/**
	 * Suppress mail logging for the current form submission context.
	 *
	 * @return void
	 */
	public function suppress_mail_logging() {
		$this->suppress_mail_logging = true;
	}

	/**
	 * Whether mail logging is suppressed for the current context.
	 *
	 * @return bool
	 */
	public function should_suppress_mail_logging() {
		return $this->suppress_mail_logging;
	}

	/**
	 * Clear current lead context.
	 *
	 * @return void
	 */
	public function clear() {
		$this->current_lead_id       = null;
		$this->current_source        = '';
		$this->suppress_mail_logging = false;
	}
}
