<?php
/**
 * RevenueForecast — expected-revenue estimates per recipe category.
 *
 * @package CartCue
 */

namespace CartCue;

defined( 'ABSPATH' ) || exit;

final class RevenueForecast {

	/**
	 * Per-category monthly-recovered-revenue estimate, in store currency.
	 *
	 * @return array<string, array{low:int,high:int,note:string}>
	 */
	public static function category_estimates(): array {
		return [
			'recover'       => [
				'low'  => 400,
				'high' => 1800,
				'note' => __( 'Abandoned-cart recovery is the highest-ROI play for most stores.', 'cartcue-sales-automation' ),
			],
			'post_purchase' => [
				'low'  => 150,
				'high' => 700,
				'note' => __( 'Post-purchase follow-ups lift repeat-order rate by 5–15%.', 'cartcue-sales-automation' ),
			],
			'win_back'      => [
				'low'  => 200,
				'high' => 900,
				'note' => __( 'Win-back nudges typically reactivate 3–8% of lapsed customers.', 'cartcue-sales-automation' ),
			],
			'loyalty'       => [
				'low'  => 50,
				'high' => 300,
				'note' => __( 'Review-request flows compound via SEO and social proof.', 'cartcue-sales-automation' ),
			],
			'subscriptions' => [
				'low'  => 250,
				'high' => 1200,
				'note' => __( 'Renewal-cycle messages reduce involuntary churn by 10–20%.', 'cartcue-sales-automation' ),
			],
			'integrations'  => [
				'low'  => 0,
				'high' => 0,
				'note' => __( 'CRM sync builds the email list that fuels every other play.', 'cartcue-sales-automation' ),
			],
		];
	}

	/**
	 * @return array{low:int,high:int,note:string,formatted:string,category:string}
	 */
	public static function for_play( array $play ): array {
		$category = self::guess_category( (string) ( $play['trigger_slug'] ?? '' ) );
		$est      = self::category_estimates()[ $category ] ?? self::category_estimates()['post_purchase'];

		$formatted = $est['high'] > 0
			? sprintf(
				'%s – %s',
				self::format_price( $est['low'] ),
				self::format_price( $est['high'] )
			)
			: __( 'helps every other play', 'cartcue-sales-automation' );

		return array_merge(
			$est,
			[
				'formatted' => $formatted,
				'category'  => $category,
			]
		);
	}

	/**
	 * @return array<string,array{low:int,high:int,formatted:string,note:string,category:string}>
	 */
	public static function for_all_recipes(): array {
		$by_trigger = [];
		foreach (
			[
				'cart_abandoned'           => 'recover',
				'checkout_started'         => 'recover',
				'payment_failed'           => 'recover',
				'order_paid'               => 'post_purchase',
				'order_status_changed'     => 'post_purchase',
				'customer_created'         => 'post_purchase',
				'customer_win_back'        => 'win_back',
				'customer_never_purchased' => 'win_back',
				'new_review'               => 'loyalty',
				'subscription_event'       => 'subscriptions',
				'subscription_cancelled'   => 'subscriptions',
			] as $slug => $cat
		) {
			$est                   = self::category_estimates()[ $cat ];
			$by_trigger[ $slug ] = array_merge(
				$est,
				[
					'formatted' => $est['high'] > 0
						? sprintf(
							'%s – %s',
							self::format_price( $est['low'] ),
							self::format_price( $est['high'] )
						)
						: '',
					'category'  => $cat,
				]
			);
		}
		return $by_trigger;
	}

	private static function format_price( int $amount ): string {
		if ( function_exists( 'wc_price' ) ) {
			return html_entity_decode( wp_strip_all_tags( wc_price( $amount ) ), ENT_QUOTES, 'UTF-8' );
		}
		return '$' . number_format_i18n( $amount );
	}

	private static function guess_category( string $trigger_slug ): string {
		if ( 0 === strpos( $trigger_slug, 'cart_' ) || 0 === strpos( $trigger_slug, 'checkout_' ) ) {
			return 'recover';
		}
		if ( 'payment_failed' === $trigger_slug ) {
			return 'recover';
		}
		if ( 0 === strpos( $trigger_slug, 'subscription_' ) ) {
			return 'subscriptions';
		}
		if ( 'customer_win_back' === $trigger_slug || 'customer_never_purchased' === $trigger_slug ) {
			return 'win_back';
		}
		if ( 'new_review' === $trigger_slug ) {
			return 'loyalty';
		}
		return 'post_purchase';
	}
}
