<?php
/**
 * Base ScoringRules
 *
 * @link       https://personalizewp.com/
 * @since      3.2.0 Merged into PWP
 * @since      1.1.0
 *
 * @package    PersonalizeWP
 * @subpackage PersonalizeWP/Scoring_Rules
 */

namespace PersonalizeWP\Scoring_Rules;

use PersonalizeWP\Model\Scoring_Rule;
use PersonalizeWP\Traits\Condition_Operator_Trait;

defined( 'ABSPATH' ) || exit; // Exit if accessed directly.

/**
 * Scoring Rules Base
 *
 * What all other Scoring Rule assigning classes extend
 */
abstract class Base {

	use Condition_Operator_Trait;

	/**
	 * Types of Activity to process, one or many.
	 *
	 * @var string[]
	 */
	protected $activity_types;

	/**
	 * Types of Scoring rules to process, one or many.
	 *
	 * Singular Scoring rules type can handle multiple Activity types.
	 *
	 * @var string[]
	 */
	protected $score_types;

	/**
	 * Stores Scoring Rules for repeated checking.
	 *
	 * @var array
	 */
	protected array $scoring_rules;

	/**
	 * Static instance.
	 *
	 * @var self
	 */
	private static $instance = null;

	/**
	 * Initialize and fetch the static instance.
	 *
	 * @since 1.1.0
	 *
	 * @return self
	 */
	public static function init() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Process an Activity record and maybe assign a Score. Type is part of the action.
	 *
	 * @since 1.1.0
	 *
	 * @param array  $activity_args Activity arguments.
	 * @param string $type          Activity Type.
	 *
	 * @return array
	 */
	abstract public function add_activity_score( $activity_args, $type );

	/**
	 * Add hooks.
	 *
	 * @since 1.1.0
	 */
	public function setup() {
		// Skip auto setup if no activity types.
		if ( empty( $this->activity_types ) ) {
			return;
		}
		add_filter( 'personalizewp_activity_record_data', array( $this, 'add_activity_score' ), 10, 2 );
	}

	/**
	 * Returns the Scoring Rules for this type.
	 *
	 * @since 1.1.0
	 */
	protected function get_scoring_rules() {
		return Scoring_Rule::get_scoring_rules( $this->score_types );
	}
}
