<?php
/**
 * Public developer API for Kramar emails.
 *
 * Usage from a theme or plugin:
 *
 *   kramar_send_email( 'kramar_custom_ask_review', $order );
 *   kramar_schedule_email( 'kramar_custom_ask_review', $order, time() + 7 * DAY_IN_SECONDS );
 *
 * @package Kramar
 * @since   1.0.0
 */

declare( strict_types=1 );

defined( 'ABSPATH' ) || exit;

/**
 * Resolve the Email Manager module instance (or null if disabled).
 *
 * @since 1.0.0
 */
function kramar_email_manager(): ?\Kramar\Modules\Email\Email_Manager {
	if ( ! class_exists( '\Kramar\Plugin' ) ) {
		return null;
	}
	foreach ( \Kramar\Plugin::get_instance()->get_modules() as $module ) {
		if ( $module instanceof \Kramar\Modules\Email\Email_Manager ) {
			return $module;
		}
	}
	return null;
}

/**
 * Send a Kramar custom email immediately.
 *
 * @since 1.0.0
 *
 * @param string                     $id      Custom email id (kramar_custom_{slug}).
 * @param \WC_Order|\WP_User|int|null $context Send context (resolves merge fields).
 * @return bool True if dispatched.
 */
function kramar_send_email( string $id, mixed $context = null ): bool {
	$manager = kramar_email_manager();
	return $manager ? $manager->send_custom_email( $id, $context ) : false;
}

/**
 * Schedule a Kramar custom email for a future timestamp (Action Scheduler).
 *
 * @since 1.0.0
 *
 * @param string                     $id        Custom email id.
 * @param \WC_Order|\WP_User|int|null $context   Send context.
 * @param int                        $timestamp Unix timestamp to send at.
 * @return bool True if scheduled.
 */
function kramar_schedule_email( string $id, mixed $context, int $timestamp ): bool {
	$manager = kramar_email_manager();
	return $manager && method_exists( $manager, 'schedule_custom_email' )
		? $manager->schedule_custom_email( $id, $context, $timestamp )
		: false;
}

// Hook aliases for do_action() callers.
add_action( 'kramar_send_email', 'kramar_send_email', 10, 2 );
add_action( 'kramar_schedule_email', 'kramar_schedule_email', 10, 3 );

/**
 * Resolve the EU Product Compliance module instance (or null if unavailable).
 *
 * @since 1.0.0
 */
function kramar_eu_product_compliance(): ?\Kramar\Modules\Compliance\EU_Product_Compliance {
	if ( ! class_exists( '\Kramar\Plugin' ) ) {
		return null;
	}
	foreach ( \Kramar\Plugin::get_instance()->get_modules() as $module ) {
		if ( $module instanceof \Kramar\Modules\Compliance\EU_Product_Compliance ) {
			return $module;
		}
	}
	return null;
}

/**
 * Get a product's resolved delivery-time label, for use in a theme template.
 *
 * Resolution order: the product's own assigned tier → the shop-wide global
 * default → empty string. Works regardless of whether the auto-display is
 * enabled, so a theme can place the label wherever it likes:
 *
 *   $levertijd = kramar_get_delivery_time( $product->get_id() );
 *   if ( $levertijd ) {
 *       echo '<span class="my-delivery">' . esc_html( $levertijd ) . '</span>';
 *   }
 *
 * @since 1.0.0
 *
 * @param int $product_id WooCommerce product ID (0 = current global $product).
 * @return string The delivery-time label, or '' when none applies.
 */
function kramar_get_delivery_time( int $product_id = 0 ): string {
	if ( 0 === $product_id ) {
		global $product;
		if ( $product instanceof \WC_Product ) {
			$product_id = $product->get_id();
		}
	}

	if ( $product_id <= 0 ) {
		return '';
	}

	$module = kramar_eu_product_compliance();
	return $module ? $module->resolve_delivery_time( $product_id ) : '';
}
