<?php
/**
 * OrderNoteAddedTrigger — fires when a WooCommerce order note is added.
 *
 * @package CartCue\Triggers
 */

namespace CartCue\Triggers;

defined( 'ABSPATH' ) || exit;

class OrderNoteAddedTrigger extends AbstractTrigger {

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

	public function get_label(): string {
		return __( 'Order Note Added', 'cartcue-sales-automation' );
	}

	public function get_sentence(): string {
		return __( 'a note is added to an order', 'cartcue-sales-automation' );
	}

	public function get_description(): string {
		return __( 'Runs when staff or the system adds an order note. Filter by note type or text.', 'cartcue-sales-automation' );
	}

	public function get_settings_fields(): array {
		return [
			[
				'key'     => 'note_type',
				'label'   => __( 'Note type', 'cartcue-sales-automation' ),
				'type'    => 'select',
				'default' => 'all',
				'options' => [
					'all'      => __( 'Any note', 'cartcue-sales-automation' ),
					'customer' => __( 'Customer note only', 'cartcue-sales-automation' ),
					'private'  => __( 'Private note only', 'cartcue-sales-automation' ),
				],
			],
			[
				'key'         => 'note_contains',
				'label'       => __( 'Note contains (optional)', 'cartcue-sales-automation' ),
				'type'        => 'text',
				'placeholder' => __( 'e.g. backorder', 'cartcue-sales-automation' ),
				'default'     => '',
			],
		];
	}

	public function register_hooks(): void {
		add_action( 'woocommerce_order_note_added', [ $this, 'handle' ], 10, 2 );
	}

	/**
	 * @param int                  $comment_id Comment ID.
	 * @param \WC_Order|false|null $order      Order object.
	 */
	public function handle( int $comment_id, $order ): void {
		if ( ! $order instanceof \WC_Order ) {
			return;
		}

		$comment = get_comment( $comment_id );
		if ( ! $comment ) {
			return;
		}

		$note_type_setting = 'all';
		$contains_setting  = '';

		foreach ( $this->plugin->runner->get_active_workflows_for_trigger( $this->get_slug() ) as $wf ) {
			$note_type_setting = sanitize_key( (string) ( $wf['trigger_settings']['note_type'] ?? 'all' ) );
			$contains_setting  = sanitize_text_field( (string) ( $wf['trigger_settings']['note_contains'] ?? '' ) );

			if ( ! $this->note_matches( $comment, $note_type_setting, $contains_setting ) ) {
				continue;
			}

			$this->plugin->runner->trigger(
				(int) $wf['id'],
				[
					'trigger'        => $this->get_slug(),
					'order_id'       => $order->get_id(),
					'customer_id'    => (int) $order->get_customer_id(),
					'customer_email' => $order->get_billing_email(),
					'note_id'        => $comment_id,
					'note_content'   => wp_strip_all_tags( (string) $comment->comment_content ),
					'note_type'      => (int) get_comment_meta( $comment_id, 'is_customer_note', true ) ? 'customer' : 'private',
				]
			);
		}
	}

	private function note_matches( \WP_Comment $comment, string $note_type, string $contains ): bool {
		$is_customer = (bool) get_comment_meta( (int) $comment->comment_ID, 'is_customer_note', true );

		if ( 'customer' === $note_type && ! $is_customer ) {
			return false;
		}
		if ( 'private' === $note_type && $is_customer ) {
			return false;
		}
		if ( '' !== $contains ) {
			$content = strtolower( wp_strip_all_tags( (string) $comment->comment_content ) );
			if ( false === strpos( $content, strtolower( $contains ) ) ) {
				return false;
			}
		}
		return true;
	}
}
