<?php
/**
 * CustomerNeverPurchasedTrigger — daily: registered customers who have never placed an order.
 *
 * @package CartCue\Triggers
 */

namespace CartCue\Triggers;

defined( 'ABSPATH' ) || exit;

class CustomerNeverPurchasedTrigger extends AbstractTrigger {

	public function get_slug(): string {
		return 'customer_never_purchased';
	}

	public function get_label(): string {
		return __( 'Customer Has Never Purchased', 'cartcue-sales-automation' );
	}

	public function get_description(): string {
		return __( 'Runs daily for registered customers with zero paid orders. Great for conversion nudges.', 'cartcue-sales-automation' );
	}

	public function get_sentence(): string {
		return __( 'a registered customer has never bought anything', 'cartcue-sales-automation' );
	}

	public function get_settings_fields(): array {
		return [
			[
				'key'     => 'min_account_age_days',
				'label'   => __( 'Minimum account age (days)', 'cartcue-sales-automation' ),
				'type'    => 'number',
				'default' => 7,
			],
			[
				'key'     => 'batch_limit',
				'label'   => __( 'Max customers per run', 'cartcue-sales-automation' ),
				'type'    => 'number',
				'default' => 200,
			],
		];
	}

	public function get_test_data(): array {
		$users = get_users( [ 'role' => 'customer', 'number' => 1, 'orderby' => 'registered', 'order' => 'DESC' ] );
		$email = ! empty( $users ) ? $users[0]->user_email : get_option( 'admin_email', '' );

		return [
			'trigger'        => $this->get_slug(),
			'customer_id'    => ! empty( $users ) ? (int) $users[0]->ID : 0,
			'customer_email' => $email,
			'_is_test'       => true,
		];
	}

	public function register_hooks(): void {}

	/**
	 * Invoked from Plugin::process_never_purchased() daily Action Scheduler hook.
	 */
	public function process_eligible_customers(): void {
		$workflows = $this->plugin->runner->get_active_workflows_for_trigger( $this->get_slug() );
		if ( empty( $workflows ) ) {
			return;
		}

		foreach ( $workflows as $workflow ) {
			$min_age = max( 0, (int) ( $workflow['trigger_settings']['min_account_age_days'] ?? 7 ) );
			$limit   = max( 1, min( 500, (int) ( $workflow['trigger_settings']['batch_limit'] ?? 200 ) ) );

			$cutoff = ( new \DateTimeImmutable( 'now', wp_timezone() ) )
				->modify( '-' . $min_age . ' days' )
				->format( 'Y-m-d H:i:s' );

			$user_query = new \WP_User_Query( [
				'role'       => 'customer',
				'fields'     => 'ID',
				'number'     => $limit,
				'orderby'    => 'user_registered',
				'order'      => 'ASC',
				'date_query' => [
					[
						'column'    => 'user_registered',
						'before'    => $cutoff,
						'inclusive' => true,
					],
				],
			] );

			$ids = $user_query->get_results();
			if ( empty( $ids ) ) {
				continue;
			}

			foreach ( $ids as $user_id ) {
				$user_id = (int) $user_id;
				if ( $user_id < 1 ) {
					continue;
				}

				if ( wc_get_customer_order_count( $user_id ) > 0 ) {
					continue;
				}

				$user = get_user_by( 'id', $user_id );
				if ( ! $user || ! is_email( $user->user_email ) ) {
					continue;
				}

				$data = [
					'trigger'        => $this->get_slug(),
					'customer_id'    => $user_id,
					'customer_email' => $user->user_email,
				];

				$this->plugin->runner->trigger( (int) $workflow['id'], $data );
			}
		}
	}
}
