<?php
/**
 * Plugin-agnostic submission value object.
 *
 * @package RewindBrevoFormBridge
 */

namespace RewindBrevoFormBridge\Adapters;

defined( 'ABSPATH' ) || exit;

/**
 * What every adapter hands the Feed Processor: no form-plugin types leak
 * past this object.
 *
 * @since 0.3.0
 */
class Submission {

	/**
	 * Adapter slug, e.g. 'forminator'.
	 *
	 * @var string
	 */
	private string $adapter;

	/**
	 * Form id (form plugins use ints and strings — normalised to string).
	 *
	 * @var string
	 */
	private string $form_id;

	/**
	 * The form plugin's entry id/reference, for traceability in the log.
	 *
	 * @var string
	 */
	private string $entry_ref;

	/**
	 * field key => value. Composite fields appear both whole ('name-1')
	 * and flattened ('name-1.first-name').
	 *
	 * @var array
	 */
	private array $fields;

	/**
	 * URL of the page the form was submitted from.
	 *
	 * @var string
	 */
	private string $source_url;

	/**
	 * Client IP at submission time.
	 *
	 * @var string
	 */
	private string $ip;

	/**
	 * Raw payload as the form plugin provided it, for the log.
	 *
	 * @var array
	 */
	private array $raw;

	/**
	 * Constructor.
	 *
	 * @since 0.3.0
	 * @param string $adapter    Adapter slug.
	 * @param string $form_id    Form id.
	 * @param string $entry_ref  Entry reference.
	 * @param array  $fields     field key => value map.
	 * @param string $source_url Source page URL.
	 * @param string $ip         Client IP.
	 * @param array  $raw        Raw payload for the log.
	 */
	public function __construct( string $adapter, string $form_id, string $entry_ref, array $fields, string $source_url, string $ip, array $raw = array() ) {
		$this->adapter    = $adapter;
		$this->form_id    = $form_id;
		$this->entry_ref  = $entry_ref;
		$this->fields     = $fields;
		$this->source_url = $source_url;
		$this->ip         = $ip;
		$this->raw        = $raw;
	}

	/**
	 * Adapter slug.
	 *
	 * @since 0.3.0
	 * @return string
	 */
	public function adapter(): string {
		return $this->adapter;
	}

	/**
	 * Form id.
	 *
	 * @since 0.3.0
	 * @return string
	 */
	public function form_id(): string {
		return $this->form_id;
	}

	/**
	 * Entry reference.
	 *
	 * @since 0.3.0
	 * @return string
	 */
	public function entry_ref(): string {
		return $this->entry_ref;
	}

	/**
	 * All field values.
	 *
	 * @since 0.3.0
	 * @return array
	 */
	public function fields(): array {
		return $this->fields;
	}

	/**
	 * One field value.
	 *
	 * @since 0.3.0
	 * @param string $key      Field key (may be a composite sub-key).
	 * @param mixed  $fallback Returned when the key is absent.
	 * @return mixed
	 */
	public function field( string $key, $fallback = null ) {
		return array_key_exists( $key, $this->fields ) ? $this->fields[ $key ] : $fallback;
	}

	/**
	 * Source page URL.
	 *
	 * @since 0.3.0
	 * @return string
	 */
	public function source_url(): string {
		return $this->source_url;
	}

	/**
	 * Client IP.
	 *
	 * @since 0.3.0
	 * @return string
	 */
	public function ip(): string {
		return $this->ip;
	}

	/**
	 * Raw payload for the log.
	 *
	 * @since 0.3.0
	 * @return array
	 */
	public function raw(): array {
		return $this->raw;
	}
}
