<?php
/**
 * AbstractAction — base class for all Autopilot actions.
 *
 * @package CartCue\Actions
 */

namespace CartCue\Actions;

use CartCue\Context;
use CartCue\Plugin;

defined( 'ABSPATH' ) || exit;

abstract class AbstractAction {

	protected Plugin $plugin;

	public function __construct( Plugin $plugin ) {
		$this->plugin = $plugin;
	}

	/** Unique identifier for this action type. */
	abstract public function get_slug(): string;

	/** Human-readable label. */
	abstract public function get_label(): string;

	/**
	 * Execute the action.
	 *
	 * @param array $settings  Action-specific settings from the workflow config.
	 * @param Context $context Mutable workflow run context.
	 * @throws \Exception On failure — caller will catch and log.
	 */
	abstract public function run( array $settings, Context $context ): void;

	/**
	 * Field definitions for the action settings panel in the workflow editor.
	 *
	 * @return array  Array of field definitions, each with:
	 *                'key', 'label', 'type' (text|textarea|select|email), 'default', ['options'].
	 */
	public function get_settings_fields(): array {
		return [];
	}

	/**
	 * Process template variables in a string.
	 *
	 * @param string $text
	 * @param Context $context
	 * @return string
	 */
	protected function process( string $text, Context $context ): string {
		return $this->plugin->variables->process( $text, $context->data );
	}

	/** Whether this run is a Test Play (no destructive side effects). */
	protected function is_test( Context $context ): bool {
		return ! empty( $context->data['_is_test'] );
	}
}
