<?php
/**
 * Shared email design tokens for WP PowerSuite plugin emails.
 *
 * @package WP_PowerSuite\Email
 * @since   1.0.0
 */

namespace WP_PowerSuite\Email;

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

/**
 * Central design tokens — compact, mobile-friendly, email-client safe.
 */
final class EmailDesign {

	public const FONT_STACK = "-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Oxygen-Sans,Ubuntu,Cantarell,'Helvetica Neue',sans-serif";

	public const MONO_STACK = "ui-monospace,SFMono-Regular,Consolas,'Liberation Mono',Menlo,monospace";

	public const MAX_WIDTH = 600;

	public const BODY_BG_DEFAULT     = '#f3f4f6';
	public const CONTENT_BG_DEFAULT  = '#ffffff';
	public const TEXT_DEFAULT        = '#374151';
	public const TEXT_MUTED_DEFAULT  = '#6b7280';
	public const FOOTER_TEXT_DEFAULT = '#787c82';
	public const BORDER_DEFAULT      = '#e5e7eb';
	public const ACCENT_DEFAULT      = '#2271b1';
	public const HIGHLIGHT_BG        = '#f9fafb';
	public const HIGHLIGHT_BORDER    = '#e5e7eb';

	public const FONT_BODY     = '14px';
	public const FONT_SMALL    = '12px';
	public const FONT_HEADING  = '18px';
	public const FONT_SUBHEAD  = '13px';
	public const FONT_BUTTON   = '14px';
	public const FONT_CODE     = '22px';

	public const PAD_OUTER_DESKTOP = '24px 16px';
	public const PAD_OUTER_MOBILE  = '16px 12px';
	public const PAD_CONTENT       = '28px 24px';
	public const PAD_CONTENT_MOB   = '20px 16px';
	public const PAD_FOOTER          = '16px 24px';

	public const RADIUS = '10px';
	public const RADIUS_BTN = '8px';

	public const LOGO_MAX_WIDTH  = 140;
	public const LOGO_MAX_HEIGHT = 48;

	/**
	 * Merge caller config with defaults (supports custom-wp-emails branding keys).
	 *
	 * @param array<string, mixed> $overrides
	 * @return array<string, string>
	 */
	public static function resolveConfig( array $overrides = [] ): array {
		$font = isset( $overrides['font_family'] ) ? trim( (string) $overrides['font_family'] ) : '';
		if ( $font === '' ) {
			$font = self::FONT_STACK;
		}

		return [
			'body_bg'              => self::sanitizeColor( (string) ( $overrides['body_bg'] ?? self::BODY_BG_DEFAULT ), self::BODY_BG_DEFAULT ),
			'content_bg'           => self::sanitizeColor( (string) ( $overrides['content_bg'] ?? self::CONTENT_BG_DEFAULT ), self::CONTENT_BG_DEFAULT ),
			'content_color'        => self::sanitizeColor( (string) ( $overrides['content_color'] ?? self::TEXT_DEFAULT ), self::TEXT_DEFAULT ),
			'footer_content_color' => self::sanitizeColor( (string) ( $overrides['footer_content_color'] ?? self::FOOTER_TEXT_DEFAULT ), self::FOOTER_TEXT_DEFAULT ),
			'highlight_bg'         => self::sanitizeColor( (string) ( $overrides['highlight_bg'] ?? self::HIGHLIGHT_BG ), self::HIGHLIGHT_BG ),
			'highlight_color'      => self::sanitizeColor( (string) ( $overrides['highlight_color'] ?? self::TEXT_DEFAULT ), self::TEXT_DEFAULT ),
			'accent_color'         => self::sanitizeColor( (string) ( $overrides['accent_color'] ?? $overrides['brand_color'] ?? self::ACCENT_DEFAULT ), self::ACCENT_DEFAULT ),
			'font_family'          => $font,
			'logo'                 => isset( $overrides['logo'] ) ? esc_url_raw( (string) $overrides['logo'] ) : '',
		];
	}

	public static function sanitizeColor( string $color, string $default ): string {
		$color = trim( $color );
		if ( preg_match( '/^#([a-fA-F0-9]{3}|[a-fA-F0-9]{6})$/', $color ) ) {
			return $color;
		}
		if ( preg_match( '/^rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/', $color, $matches ) ) {
			return sprintf(
				'rgb(%d, %d, %d)',
				min( 255, max( 0, (int) $matches[1] ) ),
				min( 255, max( 0, (int) $matches[2] ) ),
				min( 255, max( 0, (int) $matches[3] ) )
			);
		}
		return $default;
	}

	/**
	 * Minimal responsive CSS block (supported in Apple Mail, Gmail app, etc.).
	 */
	public static function responsiveStyles(): string {
		return '@media only screen and (max-width:620px){'
			. '.wpps-email-shell{width:100%!important;max-width:100%!important;}'
			. '.wpps-email-outer-pad{padding:' . self::PAD_OUTER_MOBILE . '!important;}'
			. '.wpps-email-inner-pad{padding:' . self::PAD_CONTENT_MOB . '!important;}'
			. '.wpps-email-heading{font-size:16px!important;}'
			. '.wpps-email-body-text{font-size:13px!important;}'
			. '.wpps-email-btn{display:block!important;width:100%!important;box-sizing:border-box!important;text-align:center!important;}'
			. '.wpps-email-code{font-size:20px!important;letter-spacing:3px!important;}'
			. '}';
	}
}
