<?php

namespace HaoZiTeam\AIPost\Service\Features;

defined('ABSPATH') || exit;

class AiUserIdentityService
{
	public static function random_ai_comment_author(string $task_language = ''): string
	{
		$task_language = trim((string)$task_language);
		if ($task_language !== '') {
			$tl = strtolower($task_language);
			if ($tl === 'tags-zh-fan') {
				$idioms = [
					'風和日麗', '歲月靜好', '雲淡風輕', '人間值得',
					'星辰大海', '不負韶華', '一路生花', '四海為家',
					'來日方長', '與光同塵',
				];
				$fruits = ['青檸', '草莓', '芒果', '葡萄', '橙子', '柚子', '西瓜', '櫻桃', '桃子', '藍莓'];
				$animals = ['小貓', '小狗', '兔子', '小熊', '企鵝', '海豚', '考拉', '小鹿', '刺蝟', '布偶貓'];
				$cute_suffix = ['同學', '小姐姐', '小可愛', '小朋友', '醬', '呀', '呢'];
				$desserts = ['奶茶', '汽水', '布丁', '軟糖', '泡泡', '星球', '小宇宙'];
				$nouns = ['風', '夏天', '陽光', '晚霞', '月光', '雨聲', '星光'];
				$prefix = ['小', '阿', '元氣', '奶油', '軟糖', '清醒', '晚風', '海鹽'];
				$core = ['路過', '路人', '讀者', '熱心網友', '打工人', '摸魚選手', '學習中', '隨便看看'];
				$type = (int)\wp_rand(1, 7);
				$name = '';
				switch ($type) {
					case 1:
						$name = $idioms[array_rand($idioms)];
						break;
					case 2:
						$name = $fruits[array_rand($fruits)] . $desserts[array_rand($desserts)];
						break;
					case 3:
						$name = $animals[array_rand($animals)] . $cute_suffix[array_rand($cute_suffix)];
						break;
					case 4:
						$name = $fruits[array_rand($fruits)] . '味的' . $nouns[array_rand($nouns)];
						break;
					case 5:
						$name = $prefix[array_rand($prefix)] . $core[array_rand($core)];
						break;
					case 6:
						$name = $core[array_rand($core)];
						break;
					default:
						$name = $prefix[array_rand($prefix)] . $fruits[array_rand($fruits)];
						break;
				}
				$name = trim((string)$name);
				if (\wp_rand(1, 100) <= 25) {
					$name .= (string)\wp_rand(10, 99);
				}
				return $name !== '' ? $name : '熱心網友' . (string)\wp_rand(10, 99);
			}
			if ($tl === 'tags-en' || strpos($tl, 'en') !== false) {
				$adjs = ['Sunny', 'Calm', 'Curious', 'Brave', 'Cozy', 'Quiet', 'Lucky', 'Happy', 'Mellow', 'Swift'];
				$nouns = ['Reader', 'Nomad', 'Wanderer', 'Coder', 'Gardener', 'Dreamer', 'Traveler', 'Panda', 'Fox', 'Cat'];
				$first = ['Olivia', 'Emma', 'Liam', 'Noah', 'Mia', 'Ava', 'Ethan', 'Lucas', 'Sofia', 'Leo'];
				$last = ['Smith', 'Johnson', 'Brown', 'Miller', 'Davis', 'Wilson', 'Moore', 'Taylor', 'Anderson', 'Thomas'];
				$type = (int)\wp_rand(1, 4);
				$name = '';
				if ($type === 1) {
					$name = $adjs[array_rand($adjs)] . $nouns[array_rand($nouns)];
				} elseif ($type === 2) {
					$name = $first[array_rand($first)] . ' ' . $last[array_rand($last)];
				} elseif ($type === 3) {
					$name = $first[array_rand($first)] . $nouns[array_rand($nouns)];
				} else {
					$name = $nouns[array_rand($nouns)];
				}
				if (\wp_rand(1, 100) <= 25) {
					$name .= (string)\wp_rand(10, 99);
				}
				return $name;
			}
			if ($tl === 'tags-es' || strpos($tl, 'es') !== false) {
				$adjs = ['Feliz', 'Curioso', 'Tranquilo', 'Valiente', 'Amable', 'Rapido', 'Sereno', 'Listo', 'Dulce', 'Soñador'];
				$nouns = ['Lector', 'Viajero', 'Explorador', 'Amigo', 'Gato', 'Zorro', 'Panda', 'Nube', 'Sol', 'Luna'];
				$type = (int)\wp_rand(1, 3);
				$name = '';
				if ($type === 1) {
					$name = $adjs[array_rand($adjs)] . $nouns[array_rand($nouns)];
				} elseif ($type === 2) {
					$name = $nouns[array_rand($nouns)] . $adjs[array_rand($adjs)];
				} else {
					$name = $nouns[array_rand($nouns)];
				}
				if (\wp_rand(1, 100) <= 25) {
					$name .= (string)\wp_rand(10, 99);
				}
				return $name;
			}
			if ($tl === 'tags-pt-br' || $tl === 'tags-pt-br-br' || strpos($tl, 'pt') !== false) {
				$adjs = ['Feliz', 'Curioso', 'Calmo', 'Valente', 'Gentil', 'Rapido', 'Sereno', 'Esperto', 'Doce', 'Sonhador'];
				$nouns = ['Leitor', 'Viajante', 'Explorador', 'Amigo', 'Gato', 'Raposa', 'Panda', 'Nuvem', 'Sol', 'Lua'];
				$type = (int)\wp_rand(1, 3);
				$name = '';
				if ($type === 1) {
					$name = $adjs[array_rand($adjs)] . $nouns[array_rand($nouns)];
				} elseif ($type === 2) {
					$name = $nouns[array_rand($nouns)] . $adjs[array_rand($adjs)];
				} else {
					$name = $nouns[array_rand($nouns)];
				}
				if (\wp_rand(1, 100) <= 25) {
					$name .= (string)\wp_rand(10, 99);
				}
				return $name;
			}
			if ($tl === 'tags-vi') {
				$adjs = ['Vui', 'BinhYen', 'ToMo', 'CanDam', 'DeThuong', 'NhanhNhen', 'TinhTe', 'HienLanh', 'MayMan', 'ThanThien'];
				$nouns = ['DocGia', 'NguoiDiDuLich', 'NguoiKhamPha', 'BanBe', 'Meo', 'Cao', 'GauTruc', 'May', 'MatTroi', 'MatTrang'];
				$type = (int)\wp_rand(1, 3);
				$name = '';
				if ($type === 1) {
					$name = $adjs[array_rand($adjs)] . $nouns[array_rand($nouns)];
				} elseif ($type === 2) {
					$name = $nouns[array_rand($nouns)];
				} else {
					$name = $adjs[array_rand($adjs)];
				}
				if (\wp_rand(1, 100) <= 25) {
					$name .= (string)\wp_rand(10, 99);
				}
				return $name;
			}
			if ($tl === 'tags-ja') {
				$names = ['さくら', 'ゆうき', 'はるか', 'れん', 'みお', 'りん', 'あおい', 'そら', 'ひなた', 'なな'];
				$handles = ['読者', '旅人', '勉強中', 'ねこ好き', 'いぬ好き', 'のんびり', '通りすがり', 'まったり', 'しずか', '好奇心'];
				$type = (int)\wp_rand(1, 3);
				$name = '';
				if ($type === 1) {
					$name = $names[array_rand($names)] . $handles[array_rand($handles)];
				} elseif ($type === 2) {
					$name = $handles[array_rand($handles)];
				} else {
					$name = $names[array_rand($names)];
				}
				if (\wp_rand(1, 100) <= 25) {
					$name .= (string)\wp_rand(10, 99);
				}
				return $name;
			}
			if ($tl === 'tags-ko-kr') {
				$names = ['하늘', '지민', '서준', '유나', '민준', '수아', '지우', '서연', '도윤', '다은'];
				$handles = ['독자', '여행자', '공부중', '냥집사', '댕댕이', '느긋함', '지나가던', '평온', '호기심', '따뜻함'];
				$type = (int)\wp_rand(1, 3);
				$name = '';
				if ($type === 1) {
					$name = $names[array_rand($names)] . $handles[array_rand($handles)];
				} elseif ($type === 2) {
					$name = $handles[array_rand($handles)];
				} else {
					$name = $names[array_rand($names)];
				}
				if (\wp_rand(1, 100) <= 25) {
					$name .= (string)\wp_rand(10, 99);
				}
				return $name;
			}
			if ($tl === 'tags-sa-in') {
				$first = ['Ananda', 'Saras', 'Vidya', 'Kiran', 'Aruna', 'Ravi', 'Maya', 'Sundara', 'Shanti', 'Amrita'];
				$second = ['Reader', 'Seeker', 'Friend', 'Traveler', 'Dreamer', 'Panda', 'Fox', 'Moon', 'Sun', 'Sky'];
				$type = (int)\wp_rand(1, 3);
				$name = '';
				if ($type === 1) {
					$name = $first[array_rand($first)] . $second[array_rand($second)];
				} elseif ($type === 2) {
					$name = $first[array_rand($first)];
				} else {
					$name = $second[array_rand($second)];
				}
				if (\wp_rand(1, 100) <= 25) {
					$name .= (string)\wp_rand(10, 99);
				}
				return $name;
			}
			if ($tl === 'custom') {
				$name = 'Reader';
				if (\wp_rand(1, 100) <= 25) {
					$name .= (string)\wp_rand(10, 99);
				}
				return $name;
			}
		}

		$idioms = [
			'风和日丽', '岁月静好', '云淡风轻', '人间值得',
			'星辰大海', '不负韶华', '一路生花', '四海为家',
			'来日方长', '与光同尘',
		];
		$fruits = ['青柠', '草莓', '芒果', '葡萄', '橙子', '柚子', '西瓜', '樱桃', '桃子', '蓝莓'];
		$animals = ['小猫', '小狗', '兔子', '小熊', '企鹅', '海豚', '考拉', '小鹿', '刺猬', '布偶猫'];
		$cute_suffix = ['同学', '小姐姐', '小可爱', '小朋友', '酱', '呀', '呢'];
		$desserts = ['奶茶', '汽水', '布丁', '软糖', '泡泡', '星球', '小宇宙'];
		$nouns = ['风', '夏天', '阳光', '晚霞', '月光', '雨声', '星光'];
		$prefix = ['小', '阿', '元气', '奶油', '软糖', '清醒', '晚风', '海盐'];
		$core = ['路过', '路人', '读者', '热心网友', '打工人', '摸鱼选手', '学习中', '随便看看'];

		$type = (int)\wp_rand(1, 7);
		$name = '';
		switch ($type) {
			case 1:
				$name = $idioms[array_rand($idioms)];
				break;
			case 2:
				$name = $fruits[array_rand($fruits)] . $desserts[array_rand($desserts)];
				break;
			case 3:
				$name = $animals[array_rand($animals)] . $cute_suffix[array_rand($cute_suffix)];
				break;
			case 4:
				$name = $fruits[array_rand($fruits)] . '味的' . $nouns[array_rand($nouns)];
				break;
			case 5:
				$name = $prefix[array_rand($prefix)] . $core[array_rand($core)];
				break;
			case 6:
				$name = $core[array_rand($core)];
				break;
			default:
				$name = $prefix[array_rand($prefix)] . $fruits[array_rand($fruits)];
				break;
		}

		$name = trim((string)$name);
		if (\wp_rand(1, 100) <= 25) {
			$name .= (string)\wp_rand(10, 99);
		}
		if (function_exists('mb_strlen') && \mb_strlen($name, 'UTF-8') > 12) {
			$name = \mb_substr($name, 0, 12, 'UTF-8');
		}
		return $name !== '' ? $name : '热心网友' . (string)\wp_rand(10, 99);
	}

	public static function generate_ai_comment_email(string $name): string
	{
		$host = '';
		try {
			$host = (string)\parse_url(\home_url(), PHP_URL_HOST);
		} catch (\Throwable $e) {
			$host = '';
		}
		if ($host === '') { $host = 'example.com'; }
		$local = 'aipost_' . substr(md5($name . '|' . $host), 0, 10);
		return $local . '@' . $host;
	}

	public static function generate_ai_comment_user_login(string $email): string
	{
		$email = trim((string)$email);
		if ($email === '') { $email = (string)\wp_generate_uuid4(); }
		return 'aipost_' . substr(md5($email), 0, 12);
	}
}
