<?php
/**
 * Built-in one-click rule templates. Each seeds the create form with a name +
 * ready-made rule tree the merchant can tweak before saving.
 *
 * @package SmartCategories
 */

defined( 'ABSPATH' ) || exit;

/**
 * Class SCWC_Templates
 */
class SCWC_Templates {

	/**
	 * The template catalog.
	 *
	 * @return array<int,array<string,mixed>>
	 */
	public static function all() {
		$templates = array(
			self::tpl(
				'new-arrivals',
				__( 'New Arrivals', 'smart-categories-for-woocommerce' ),
				__( 'Products added in the last 30 days.', 'smart-categories-for-woocommerce' ),
				self::group( 'all', array( self::cond( 'date_created', 'in_last_days', '30' ) ) )
			),
			self::tpl(
				'best-sellers',
				__( 'Best Sellers', 'smart-categories-for-woocommerce' ),
				__( 'All-time top sellers (10+ units sold).', 'smart-categories-for-woocommerce' ),
				self::group( 'all', array( self::cond( 'total_sales', 'gte', '10' ) ) )
			),
			self::tpl(
				'low-stock',
				__( 'Low Stock', 'smart-categories-for-woocommerce' ),
				__( 'Products running low (1–5 units) — great for urgency badges.', 'smart-categories-for-woocommerce' ),
				self::group( 'all', array( self::cond( 'stock_quantity', 'between', array( '1', '5' ) ) ) )
			),
			self::tpl(
				'on-sale',
				__( 'On Sale', 'smart-categories-for-woocommerce' ),
				__( 'All products currently on sale.', 'smart-categories-for-woocommerce' ),
				self::group( 'all', array( self::cond( 'on_sale', 'is_true', '' ) ) )
			),
			self::tpl(
				'big-discount',
				__( 'Big Discount (Outlet)', 'smart-categories-for-woocommerce' ),
				__( 'Products discounted 40% or more.', 'smart-categories-for-woocommerce' ),
				self::group( 'all', array( self::cond( 'discount_percentage', 'gte', '40' ) ) )
			),
			self::tpl(
				'back-in-stock',
				__( 'Back in Stock', 'smart-categories-for-woocommerce' ),
				__( 'Products updated in the last 7 days that are back in stock.', 'smart-categories-for-woocommerce' ),
				self::group(
					'all',
					array(
						self::cond( 'stock_status', 'equals', 'instock' ),
						self::cond( 'date_modified', 'in_last_days', '7' ),
					)
				)
			),
			self::tpl(
				'featured',
				__( 'Featured', 'smart-categories-for-woocommerce' ),
				__( 'Products marked as featured.', 'smart-categories-for-woocommerce' ),
				self::group( 'all', array( self::cond( 'featured', 'is_true', '' ) ) )
			),
			self::tpl(
				'premium',
				__( 'Premium (High Price)', 'smart-categories-for-woocommerce' ),
				__( 'Products above a set price.', 'smart-categories-for-woocommerce' ),
				self::group( 'all', array( self::cond( 'price', 'gte', '1000' ) ) )
			),
			self::tpl(
				'out-of-stock',
				__( 'Sold Out', 'smart-categories-for-woocommerce' ),
				__( 'Out-of-stock products.', 'smart-categories-for-woocommerce' ),
				self::group( 'all', array( self::cond( 'stock_status', 'equals', 'outofstock' ) ) )
			),
			self::tpl(
				'missing-image',
				__( 'Missing Image', 'smart-categories-for-woocommerce' ),
				__( 'Products missing an image (for catalog maintenance).', 'smart-categories-for-woocommerce' ),
				self::group( 'all', array( self::cond( 'has_image', 'is_false', '' ) ) )
			),
		);


		/**
		 * Filter the built-in templates.
		 *
		 * @param array $templates Template list.
		 */
		return apply_filters( 'scwc_templates', $templates );
	}

	/**
	 * Build a template entry.
	 *
	 * @param string $slug  Slug.
	 * @param string $name  Name.
	 * @param string $desc  Description.
	 * @param array  $rules Rule tree.
	 * @return array
	 */
	private static function tpl( $slug, $name, $desc, $rules ) {
		return array(
			'slug'        => $slug,
			'name'        => $name,
			'description' => $desc,
			'rules'       => $rules,
		);
	}

	/**
	 * Build a group node.
	 *
	 * @param string $match    all|any.
	 * @param array  $children Children.
	 * @param bool   $not      Negate.
	 * @return array
	 */
	private static function group( $match, $children, $not = false ) {
		return array(
			'type'     => 'group',
			'match'    => $match,
			'not'      => $not,
			'children' => $children,
		);
	}

	/**
	 * Build a condition node.
	 *
	 * @param string $field    Field key.
	 * @param string $operator Operator.
	 * @param mixed  $value    Value.
	 * @return array
	 */
	private static function cond( $field, $operator, $value ) {
		return array(
			'type'     => 'condition',
			'field'    => $field,
			'operator' => $operator,
			'value'    => $value,
		);
	}
}
