<?php

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

class AIEO_Attribution_Block {

	const ACCENT_SITE        = '#1565c0';
	const ACCENT_MARKETPLACE = '#7b2ff7';

	
	public static function utm_rows( array $a ) {
		$rows = array();
		foreach ( array( 'utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term' ) as $p ) {
			$v          = isset( $a[ $p ] ) ? trim( (string) $a[ $p ] ) : '';
			$rows[ $p ] = '' !== $v ? $v : '—';
		}
		return $rows;
	}

	
	public static function channel_label( $channel ) {
		$channel = strtolower( trim( (string) $channel ) );
		if ( '' === $channel ) {
			return __( 'Unknown (not yet attributed)', 'ai-eshop-optimizer' );
		}
		$map = array(
			'paid_google'    => 'Paid · Google Ads',
			'paid_facebook'  => 'Paid · Facebook / Instagram',
			'paid_tiktok'    => 'Paid · TikTok',
			'paid_microsoft' => 'Paid · Microsoft Ads',
			'organic_search' => 'Organic search',
			'social'         => 'Social',
			'newsletter'     => 'Newsletter / Email',
			'web_push'       => 'Web push',
			'referral'       => 'Referral',
			'direct'         => 'Direct',
			'internal'       => 'Internal',
			'phone_orders'   => 'Phone Orders',
		);
		if ( isset( $map[ $channel ] ) ) {
			return $map[ $channel ];
		}
		return ucwords( str_replace( array( '_', '-' ), ' ', $channel ) );
	}

	
	public static function render_html( $title, array $rows, $accent = self::ACCENT_SITE ) {
		$h  = '<table cellspacing="0" cellpadding="6" border="0" style="width:100%;margin:0 0 18px;border:1px solid ' . $accent . ';border-collapse:separate;border-radius:6px;">';
		$h .= '<tr><td colspan="2" style="background:' . $accent . ';color:#ffffff;font-weight:bold;padding:8px 10px;">' . esc_html( $title ) . '</td></tr>';
		foreach ( $rows as $k => $v ) {
			$h .= '<tr><td style="font-weight:bold;color:#555555;width:170px;vertical-align:top;padding:6px 10px;">' . esc_html( $k ) . '</td><td style="color:#111111;padding:6px 10px;word-break:break-all;">' . esc_html( $v ) . '</td></tr>';
		}
		$h .= '</table>';
		return $h;
	}

	
	public static function render_plain( $title, array $rows ) {
		$out = '== ' . $title . " ==\n";
		foreach ( $rows as $k => $v ) {
			$out .= $k . ': ' . $v . "\n";
		}
		return $out;
	}
}
