<?php
/**
 * Demo Data
 *
 * Centralized fake affiliate data for empty states, onboarding,
 * and demo UI across AffiliateWP admin screens.
 *
 * @package     AffiliateWP
 * @subpackage  Utils
 * @copyright   Copyright (c) 2025, Sandhills Development, LLC
 * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
 * @since       2.33.0
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Returns an array of demo affiliate profiles.
 *
 * Gender-balanced, ethnically diverse, with realistic data.
 * Use these across all empty states, onboarding screens,
 * and demo UI to maintain consistency.
 *
 * @since 2.33.0
 *
 * @param int $count Number of demo affiliates to return. Default 0 (all).
 * @return array Array of demo affiliate arrays.
 */
function affwp_get_demo_affiliates( $count = 0 ) {

	$affiliates = [
		[
			'name'           => 'Marcus Johnson',
			'initials'       => 'MJ',
			'avatar'         => 'avatar-9.webp',
			'username'       => 'marcusj',
			'email'          => 'mjohnson@gmail.com',
			'referrals'      => 74,
			'earnings'       => 1890.50,
			'status'         => 'active',
			'group'          => 'Partners',
			'social_handles' => [
				'x'         => 'marcusj',
				'instagram' => 'marcus.j',
				'tiktok'    => 'marcusjohnson',
				'linkedin'  => 'marcus-johnson',
				'facebook'  => 'MarcusJohnsonCreates',
			],
		],
		[
			'name'           => 'Elsa Lindgren',
			'initials'       => 'EL',
			'avatar'         => 'avatar-2.webp',
			'username'       => 'elsa_l',
			'email'          => 'elsa.lindgren@outlook.com',
			'referrals'      => 132,
			'earnings'       => 3420.50,
			'status'         => 'active',
			'group'          => 'VIP',
			'social_handles' => [
				'x'         => 'elsalindgren',
				'instagram' => 'elsa.lindgren',
				'tiktok'    => 'elsa.l',
				'linkedin'  => 'elsa-lindgren',
				'facebook'  => 'ElsaLindgrenOfficial',
			],
		],
		[
			'name'           => 'Carlos Rivera',
			'initials'       => 'CR',
			'avatar'         => 'avatar-5.webp',
			'username'       => 'carlos_r',
			'email'          => 'carlos.rivera@gmail.com',
			'referrals'      => 63,
			'earnings'       => 1540.00,
			'status'         => 'active',
			'group'          => 'Partners',
			'social_handles' => [
				'x'         => 'carlosriv',
				'instagram' => 'carlos.rivera',
				'tiktok'    => 'carlosrivera',
				'linkedin'  => 'carlos-rivera',
				'facebook'  => 'CarlosRiveraReviews',
			],
		],
		[
			'name'           => 'Yuki Tanaka',
			'initials'       => 'YT',
			'avatar'         => 'avatar-3.webp',
			'username'       => 'yukit',
			'email'          => 'ytanaka@lumenmedia.com',
			'referrals'      => 89,
			'earnings'       => 2180.00,
			'status'         => 'active',
			'group'          => 'Partners',
			'social_handles' => [
				'x'         => 'yukitanaka',
				'instagram' => 'yuki.tanaka',
				'tiktok'    => 'yuki.t',
				'linkedin'  => 'yuki-tanaka',
				'facebook'  => 'YukiTanakaCreates',
			],
		],
		[
			'name'           => 'James Mercer',
			'initials'       => 'JM',
			'avatar'         => 'avatar-6.webp',
			'username'       => 'jamesm',
			'email'          => 'james.mercer@yahoo.com',
			'referrals'      => 28,
			'earnings'       => 720.00,
			'status'         => 'active',
			'group'          => '',
			'social_handles' => [
				'x'         => 'jamesmercer',
				'instagram' => 'james.mercer',
				'tiktok'    => 'jamesm',
				'linkedin'  => 'james-mercer',
				'facebook'  => 'JamesMercerOfficial',
			],
		],
		[
			'name'           => 'Jess Calloway',
			'initials'       => 'JC',
			'avatar'         => 'avatar-4.webp',
			'username'       => 'jessc',
			'email'          => 'jess@callowaymedia.co',
			'referrals'      => 215,
			'earnings'       => 5890.75,
			'status'         => 'active',
			'group'          => 'VIP',
			'social_handles' => [
				'x'         => 'jessc',
				'instagram' => 'jess.calloway',
				'tiktok'    => 'jess.calloway',
				'linkedin'  => 'jess-calloway',
				'facebook'  => 'JessCallowayCreates',
			],
		],
		[
			'name'           => 'Ryan Brooks',
			'initials'       => 'RB',
			'avatar'         => 'avatar-1.webp',
			'username'       => 'ryanb',
			'email'          => 'ryanbrooks@icloud.com',
			'referrals'      => 47,
			'earnings'       => 1250.00,
			'status'         => 'active',
			'group'          => 'VIP',
			'social_handles' => [
				'x'         => 'ryanbrooks',
				'instagram' => 'ryan.brooks',
				'tiktok'    => 'ryanb',
				'linkedin'  => 'ryan-brooks',
				'facebook'  => 'RyanBrooksReviews',
			],
		],
		[
			'name'           => 'Farah Nazari',
			'initials'       => 'FN',
			'avatar'         => 'avatar-7.webp',
			'username'       => 'farah_n',
			'email'          => 'farah.nazari@gmail.com',
			'referrals'      => 156,
			'earnings'       => 4200.25,
			'status'         => 'active',
			'group'          => 'VIP',
			'social_handles' => [
				'x'         => 'farahnazari',
				'instagram' => 'farah.nazari',
				'tiktok'    => 'farah.n',
				'linkedin'  => 'farah-nazari',
				'facebook'  => 'FarahNazariOfficial',
			],
		],
		[
			'name'           => 'Kenji Sato',
			'initials'       => 'KS',
			'avatar'         => 'avatar-8.webp',
			'username'       => 'kenjis',
			'email'          => 'kenji.s@proton.me',
			'referrals'      => 5,
			'earnings'       => 125.00,
			'status'         => 'pending',
			'group'          => '',
			'social_handles' => [
				'x'         => 'kenjisato',
				'instagram' => 'kenji.sato',
				'tiktok'    => 'kenji.s',
				'linkedin'  => 'kenji-sato',
				'facebook'  => 'KenjiSatoCreates',
			],
		],
		[
			'name'           => 'Priya Sharma',
			'initials'       => 'PS',
			'avatar'         => 'avatar-10.webp',
			'username'       => 'priya_s',
			'email'          => 'priyasharma88@hotmail.com',
			'referrals'      => 41,
			'earnings'       => 980.00,
			'status'         => 'active',
			'group'          => '',
			'social_handles' => [
				'x'         => 'priyasharma',
				'instagram' => 'priya.sharma',
				'tiktok'    => 'priya.s',
				'linkedin'  => 'priya-sharma',
				'facebook'  => 'PriyaSharmaOfficial',
			],
		],
	];

	if ( $count > 0 ) {
		return array_slice( $affiliates, 0, $count );
	}

	return $affiliates;
}

/**
 * Returns the full URL for a demo avatar image.
 *
 * @since 2.33.0
 *
 * @param string $filename Avatar filename (e.g. 'avatar-1.webp').
 * @return string Full URL to the demo avatar image.
 */
function affwp_get_demo_avatar_url( $filename ) {
	return AFFILIATEWP_PLUGIN_URL . 'assets/images/demo/' . $filename;
}

/**
 * Returns the canonical demo affiliate used as the persona across all
 * creative-type previews (social_post, image, dynamic_image, etc.).
 *
 * Centralized so the same fictional affiliate appears consistently in
 * every preview surface — merchants get one recognizable person to
 * anchor their evaluation against, instead of a different face per
 * creative type.
 *
 * Currently returns the first entry in the affiliate roster. To swap
 * which affiliate is used as the canonical persona, reorder the array
 * in affwp_get_demo_affiliates() so the desired affiliate is first.
 *
 * @since 2.33.0
 *
 * @return array Single demo affiliate associative array, including
 *               the social_handles map.
 */
function affwp_get_demo_creative_persona() {
	$affiliates = affwp_get_demo_affiliates( 1 );
	return $affiliates[0] ?? [];
}

/**
 * Returns demo avatar stack HTML for use in empty states.
 *
 * Renders a fixed set of demo affiliate avatars as an overlapping stack.
 * Uses external placeholder images until local demo images are saved.
 *
 * @since 2.33.0
 *
 * @param int $count   Number of visible avatars. Default 5.
 * @param int $overflow The "+N" overflow count to display. Default 7.
 * @return string Avatar stack HTML.
 */
function affwp_get_demo_avatar_stack( $count = 5, $overflow = 7 ) {
	$affiliates = affwp_get_demo_affiliates( $count );
	$avatars    = '';
	$mask_class = '-ml-0.5 [mask-image:radial-gradient(circle_at_-12px_50%,transparent_16px,black_16.5px)]';
	$index      = 0;

	foreach ( $affiliates as $person ) {
		$url = affwp_get_demo_avatar_url( $person['avatar'] );

		$img = sprintf(
			'<img src="%s" alt="%s" class="size-7 rounded-full bg-gray-50 object-cover" loading="lazy" />',
			esc_url( $url ),
			esc_attr( $person['name'] )
		);

		if ( 0 === $index ) {
			$avatars .= $img;
		} else {
			$avatars .= sprintf( '<span class="%s">%s</span>', esc_attr( $mask_class ), $img );
		}

		$index++;
	}

	$html  = '<span class="inline-flex items-center align-middle">';
	$html .= '<span class="flex items-center">' . $avatars . '</span>';

	if ( $overflow > 0 ) {
		$html .= sprintf( '<span class="ml-1.5 text-xs text-gray-400">+%d</span>', $overflow );
	}

	$html .= '</span>';

	return $html;
}
