<?php
/**
 * Email helper functions.
 *
 * @package Awesomemotive\Rewardswp\Helpers
 * @since 1.0.0
 */

namespace Awesomemotive\Rewardswp\Helpers;

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

use Awesomemotive\Rewardswp\Controllers\Admin\EmailsAdminController;

/**
 * Get email context labels.
 *
 * @since 1.1.0
 *
 * @return array Array of context => label pairs.
 */
function get_email_context_labels(): array {
	return [
		'member'   => esc_html__( 'Members', 'rewardswp' ),
		'referral' => esc_html__( 'Referrals', 'rewardswp' ),
		'points'   => esc_html__( 'Points', 'rewardswp' ),
		'order'    => esc_html__( 'Orders', 'rewardswp' ),
	];
}

/**
 * Get email context label by context key.
 *
 * @since 1.1.0
 *
 * @param string $context The context key.
 *
 * @return string The localized context label.
 */
function get_email_context_label( string $context ): string {
	$labels = get_email_context_labels();
	return $labels[ $context ] ?? ucfirst( $context );
}

/**
 * Get all available email contexts.
 *
 * @since 1.1.0
 *
 * @return array Array of context keys.
 */
function get_email_contexts(): array {
	return array_keys( get_email_context_labels() );
}

/**
 * Get email context description.
 *
 * @since 1.1.0
 *
 * @param string $context The context key.
 *
 * @return string The context description.
 */
function get_email_context_description( string $context ): string {
	$descriptions = [
		'member'   => esc_html__( 'Emails sent when users interact with their accounts', 'rewardswp' ),
		'referral' => esc_html__( 'Emails related to the referral program activities', 'rewardswp' ),
		'points'   => esc_html__( 'Emails sent for points-related activities', 'rewardswp' ),
		'order'    => esc_html__( 'Emails sent during order and redemption processes', 'rewardswp' ),
	];

	return $descriptions[ $context ] ?? '';
}

/**
 * Get email by name using global container.
 *
 * @since 1.1.0
 *
 * @param string $name The email name.
 *
 * @return \Awesomemotive\Rewardswp\Models\Email|null
 */
function get_email_by_name( string $name ) {
	try {
		$emails_service = am_rewardswp()->get_container()->get( \Awesomemotive\Rewardswp\Services\EmailsService::class );
		return $emails_service->get_email_by_name( $name );
	} catch ( \Exception $e ) {
		rewardswp_log_error(
			'EmailHelper failed to fetch email by name: {message}',
			[
				'message' => $e->getMessage(),
			]
		);
		return null;
	}
}

/**
 * Get logo data for an email.
 *
 * @since 1.1.0
 *
 * @param string $email_name The email name.
 *
 * @return array|null Logo data array or null if no logo.
 */
function get_email_banner_data( string $email_name ): ?array {
	$email = get_email_by_name( $email_name );

	if ( ! $email ) {
		return null;
	}

	// Prefer banner_* meta, fallback to logo_* for BC
	$logo_attachment_id = (int) ( $email->get_meta( 'banner_attachment_id', true ) ?: $email->get_meta( 'logo_attachment_id', true ) );
	$logo_size          = $email->get_meta( 'banner_size', true ) ?: ( $email->get_meta( 'logo_size', true ) ?: \Awesomemotive\Rewardswp\Services\EmailsService::EMAIL_BANNER_DEFAULT_SIZE );

	if ( ! $logo_attachment_id ) {
		return null;
	}

	$image_data = wp_get_attachment_image_src( $logo_attachment_id, $logo_size );

	if ( ! $image_data ) {
		return null;
	}

	return [
		'attachment_id' => $logo_attachment_id,
		'url'           => $image_data[0],
		'width'         => $image_data[1],
		'height'        => $image_data[2],
		'size'          => $logo_size,
		'alt'           => get_post_meta( $logo_attachment_id, '_wp_attachment_image_alt', true ) ?: get_bloginfo( 'name' ),
	];
}

/**
 * Get email logo data from global settings.
 *
 * @since 1.1.0
 *
 * @return array|null Logo data or null if none is set.
 */
function get_email_logo_data(): ?array {
	$logo_data = get_option_value( \Awesomemotive\Rewardswp\Settings\Options\Emails\EmailLogoOption::class );

	if ( empty( $logo_data ) || ! is_array( $logo_data ) ) {
		return null;
	}

	$attachment_id = (int) ( $logo_data['attachment_id'] ?? 0 );
	$size          = $logo_data['size'] ?? 'medium';

	if ( ! $attachment_id ) {
		return null;
	}

	$image_data = wp_get_attachment_image_src( $attachment_id, $size );

	if ( ! $image_data ) {
		return null;
	}

	// Force HTTPS if current site is HTTPS to avoid Mixed Content issues
	$url = $image_data[0];
	if ( is_ssl() && strpos( $url, 'http://' ) === 0 ) {
		$url = str_replace( 'http://', 'https://', $url );
	}

	return [
		'attachment_id' => $attachment_id,
		'url'           => $url,
		'width'         => $image_data[1],
		'height'        => $image_data[2],
		'size'          => $size,
		'alt'           => get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ?: get_bloginfo( 'name' ),
	];
}
