<?php
/**
 * Subscription Triggers Trait.
 *
 * @package automation-web-platform
 */

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

trait WAWP_Subscription_Triggers {
	/**
	 * Handle subscription status updated trigger.
	 *
	 * @param WC_Subscription $subscription Subscription object.
	 * @param string          $new_status   New status.
	 * @param string          $old_status   Old status.
	 */
	public function handle_subscription_status_updated( $subscription, $new_status, $old_status ) {
		if ( empty( $this->wawp_db_manager ) || ! class_exists( 'WC_Subscription' ) || ! ( $subscription instanceof WC_Subscription ) ) {
			return;
		}

		$settings    = $this->get_settings();
		$user_id     = $subscription->get_customer_id();
		$locale      = $user_id ? get_user_locale( $user_id ) : ( $settings['main_language_code'] ? $settings['main_language_code'] : 'en_US' );
		$trigger_key = 'wc_sub_status_' . $new_status;
		$rules       = $this->get_matching_rules( $settings, $locale, $trigger_key );
		if ( empty( $rules ) ) {
			return;
		}

		$billing_phone = $subscription->get_billing_phone();
		if ( empty( $billing_phone ) && $user_id ) {
			$billing_phone = WAWP()->user_manager->get_user_phone( $user_id );
		}

		$all_statuses  = function_exists( 'wcs_get_subscription_statuses' ) ? wcs_get_subscription_statuses() : array();
		$new_label     = isset( $all_statuses[ 'wc-' . $new_status ] ) ? $all_statuses[ 'wc-' . $new_status ] : $new_status;
		$old_label     = isset( $all_statuses[ 'wc-' . $old_status ] ) ? $all_statuses[ 'wc-' . $old_status ] : $old_status;
		$customer_name = $subscription->get_formatted_billing_full_name() ? $subscription->get_formatted_billing_full_name() : ( $user_id ? get_userdata( $user_id )->display_name : esc_html__( 'Customer', 'automation-web-platform' ) );

		$replacements_user = array(
			'{{subscription_id}}'               => $subscription->get_id(),
			'{{customer_name}}'                 => $customer_name,
			'{{user_email}}'                    => $subscription->get_billing_email(),
			'{{billing_email}}'                 => $subscription->get_billing_email(),
			'{{subscription_status_label}}'     => $new_label,
			'{{old_subscription_status_label}}' => $old_label,
			'{{subscription_total}}'            => html_entity_decode( wp_strip_all_tags( $subscription->get_formatted_order_total() ), ENT_QUOTES, 'UTF-8' ),
			'{{next_payment_date}}'             => $subscription->get_date_to_display( 'next_payment' ),
			'{{sitename}}'                      => get_bloginfo( 'name' ),
		);

		$recipient_user = array(
			'user_id'      => $user_id,
			'email'        => $subscription->get_billing_email(),
			'phone'        => $billing_phone,
			'display_name' => $customer_name,
		);

		$log_base = sprintf( 'Subscription #%s Status %s - ', $subscription->get_id(), $new_label );
		foreach ( $rules as $rule ) {
			$this->schedule_or_send( $rule, $replacements_user, $recipient_user, $log_base, $subscription->get_id(), $trigger_key );
		}
	}

	/**
	 * Handle subscription renewal complete trigger.
	 *
	 * @param WC_Subscription $subscription Subscription object.
	 * @param WC_Order        $last_order   Last renewal order.
	 */
	public function handle_subscription_renewal_complete( $subscription, $last_order ) {
		if ( empty( $this->wawp_db_manager ) || ! class_exists( 'WC_Subscription' ) || ! ( $subscription instanceof WC_Subscription ) || ! ( $last_order instanceof WC_Order ) ) {
			return;
		}

		$settings    = $this->get_settings();
		$user_id     = $subscription->get_customer_id();
		$locale      = $user_id ? get_user_locale( $user_id ) : ( $settings['main_language_code'] ? $settings['main_language_code'] : 'en_US' );
		$trigger_key = 'wc_sub_renewal_payment_complete';
		$rules       = $this->get_matching_rules( $settings, $locale, $trigger_key );
		if ( empty( $rules ) ) {
			return;
		}

		$billing_phone = $subscription->get_billing_phone();
		if ( empty( $billing_phone ) && $user_id ) {
			$billing_phone = WAWP()->user_manager->get_user_phone( $user_id );
		}

		$customer_name = $subscription->get_formatted_billing_full_name() ? $subscription->get_formatted_billing_full_name() : ( $user_id ? get_userdata( $user_id )->display_name : esc_html__( 'Customer', 'automation-web-platform' ) );

		$replacements_user = array(
			'{{subscription_id}}'    => $subscription->get_id(),
			'{{renewal_order_id}}'   => $last_order->get_id(),
			'{{customer_name}}'      => $customer_name,
			'{{user_email}}'         => $subscription->get_billing_email(),
			'{{billing_email}}'      => $subscription->get_billing_email(),
			'{{renewal_amount}}'     => html_entity_decode( wp_strip_all_tags( $last_order->get_formatted_order_total() ), ENT_QUOTES, 'UTF-8' ),
			'{{subscription_total}}' => html_entity_decode( wp_strip_all_tags( $subscription->get_formatted_order_total() ), ENT_QUOTES, 'UTF-8' ),
			'{{next_payment_date}}'  => $subscription->get_date_to_display( 'next_payment' ),
			'{{sitename}}'           => get_bloginfo( 'name' ),
		);

		$recipient_user = array(
			'user_id'      => $user_id,
			'email'        => $subscription->get_billing_email(),
			'phone'        => $billing_phone,
			'display_name' => $customer_name,
		);

		$log_base = sprintf( 'Subscription #%s Renewal (Order #%s)', $subscription->get_id(), $last_order->get_id() );
		foreach ( $rules as $rule ) {
			$this->schedule_or_send( $rule, $replacements_user, $recipient_user, $log_base, $last_order->get_id(), $trigger_key );
		}
	}
}
