<?php
/**
 * SMS sender.
 *
 * @package CartNinjaRecovery
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! interface_exists( 'CARTNINJA_Gateway_Interface' ) ) {
	interface CARTNINJA_Gateway_Interface {
		public function send( $to, $message );
	}
}

class CARTNINJA_SMS_Sender {

	private $gateway;

	public function __construct( CARTNINJA_Gateway_Interface $gateway ) {
		$this->gateway = $gateway;
	}

	public function send_cart_sms( $cart, $context = array() ) {
		$context = wp_parse_args(
			$context,
			array(
				'customer_name' => $this->resolve_customer_name(),
				'cart_total'    => $this->format_total( $cart ),
				'cart_url'      => $this->build_recovery_url( $cart ),
				'site_name'     => wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ),
				'coupon_code'   => isset( $cart['coupon_code'] ) ? sanitize_text_field( $cart['coupon_code'] ) : '',
				'sms_template'  => '',
			)
		);

		$message = $this->render_message( $context );
		$result  = $this->gateway->send( sanitize_text_field( $cart['phone'] ), $message );

		$result['message'] = $message;

		return $result;
	}

	public function render_preview() {
		return $this->render_message(
			array(
				'customer_name' => __( 'Rahul', 'cartninja-recovery-for-woocommerce' ),
				'cart_total'    => '₹1,499.00',
				'cart_url'      => home_url( '/cart/?cartninja_token=demo123' ),
				'site_name'     => wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ),
				'coupon_code'   => 'RECOVER-ABCD1234',
			)
		);
	}

	public function render_message( $context ) {
		$settings = get_option( 'cartninja_settings', array() );
		$message  = ! empty( $context['sms_template'] ) ? $context['sms_template'] : ( isset( $settings['message']['sms_template'] ) ? $settings['message']['sms_template'] : '' );

		if ( empty( $message ) ) {
			$message = __( 'Hi {{customer_name}}, you left items worth {{cart_total}} in your cart at {{site_name}}. Complete your purchase here: {{cart_url}}', 'cartninja-recovery-for-woocommerce' );
		}

		return strtr(
			$message,
			array(
				'{{customer_name}}' => sanitize_text_field( $context['customer_name'] ),
				'{{cart_total}}'    => sanitize_text_field( $context['cart_total'] ),
				'{{cart_url}}'      => esc_url_raw( $context['cart_url'] ),
				'{{site_name}}'     => sanitize_text_field( $context['site_name'] ),
				'{{coupon_code}}'   => sanitize_text_field( $context['coupon_code'] ),
			)
		);
	}

	private function build_recovery_url( $cart ) {
		$settings = get_option( 'cartninja_settings', array() );
		$general  = isset( $settings['general'] ) ? $settings['general'] : array();
		$base_url = ! empty( $general['recovery_url_base'] ) ? $general['recovery_url_base'] : home_url( '/cart/' );

		return add_query_arg( 'cartninja_token', sanitize_text_field( $cart['recovery_token'] ), $base_url );
	}

	private function resolve_customer_name() {
		if ( function_exists( 'WC' ) && WC() && WC()->session ) {
			$name = WC()->session->get( 'cartninja_customer_name' );
			if ( ! empty( $name ) ) {
				return sanitize_text_field( $name );
			}
		}

		return __( 'there', 'cartninja-recovery-for-woocommerce' );
	}

	private function format_total( $cart ) {
		$total = isset( $cart['cart_total'] ) ? (float) $cart['cart_total'] : 0;

		if ( function_exists( 'wc_price' ) ) {
			return sanitize_text_field( wp_strip_all_tags( wc_price( $total ) ) );
		}

		return sanitize_text_field( number_format_i18n( $total, 2 ) );
	}
}
