<?php
defined( 'ABSPATH' ) || exit;

if ( ! isset( $avi_lang ) || ! is_string( $avi_lang ) ) {
	$avi_lang = 'en';
}
if ( ! in_array( $avi_lang, array( 'en', 'fr', 'es' ), true ) ) {
	$avi_lang = 'en';
}
if ( ! isset( $avi_terms_mode ) || ! is_string( $avi_terms_mode ) ) {
	$avi_terms_mode = 'standard';
}

$_tc_links = array(
	'en' => home_url( '/terms-of-use/' ),
	'fr' => home_url( '/fr/mentions-legales/' ),
	'es' => home_url( '/es/legal/' ),
);
$_tc_link = isset( $_tc_links[ $avi_lang ] ) ? $_tc_links[ $avi_lang ] : $_tc_links['en'];

$_amb_links = array(
	'en' => home_url( '/ambassador-program/' ),
	'fr' => home_url( '/fr/programme-ambassadrice/' ),
	'es' => home_url( '/es/programa-embajadora/' ),
);
$_amb_link = isset( $_amb_links[ $avi_lang ] ) ? $_amb_links[ $avi_lang ] : $_amb_links['en'];

$_tc_texts = array(
	'en' => 'I have read and accept the <a href="%s" target="_blank" rel="noopener">Terms &amp; Conditions</a>',
	'fr' => 'J\'ai lu et j\'accepte les <a href="%s" target="_blank" rel="noopener">Conditions Générales d\'Utilisation</a>',
	'es' => 'He leído y acepto los <a href="%s" target="_blank" rel="noopener">Términos y Condiciones</a>',
);

$_amb_texts = array(
	'en' => 'I understand and accept the <a href="%s" target="_blank" rel="noopener">Ambassador Program rules</a> (referral rewards, eligibility conditions)',
	'fr' => 'Je comprends et j\'accepte les <a href="%s" target="_blank" rel="noopener">règles du Programme Ambassadeur</a> (récompenses de parrainage, conditions d\'éligibilité)',
	'es' => 'Entiendo y acepto las <a href="%s" target="_blank" rel="noopener">reglas del Programa Embajador</a> (recompensas por referidos, condiciones de elegibilidad)',
);

$_tc_label = sprintf(
	isset( $_tc_texts[ $avi_lang ] ) ? $_tc_texts[ $avi_lang ] : $_tc_texts['en'],
	esc_url( $_tc_link )
);

$_amb_label = sprintf(
	isset( $_amb_texts[ $avi_lang ] ) ? $_amb_texts[ $avi_lang ] : $_amb_texts['en'],
	esc_url( $_amb_link )
);

$_tc_kses = array(
	'a' => array(
		'href'   => array(),
		'target' => array(),
		'rel'    => array(),
	),
);

if ( empty( $GLOBALS['avi_terms_css_done'] ) ) {
	$GLOBALS['avi_terms_css_done'] = true;
	?>
<style>
.avi-terms-wrap {
	margin: 16px 0;
	display: flex;
	flex-direction: column;
	gap: 8px;
}
.avi-terms-label {
	display: flex;
	align-items: flex-start;
	gap: 10px;
	cursor: pointer;
	font-size: 13px;
	color: #94a3b8;
	line-height: 1.6;
}
.avi-terms-label input[type="checkbox"] {
	margin-top: 3px;
	flex-shrink: 0;
	accent-color: #DAA520;
	width: 16px;
	height: 16px;
}
.avi-terms-label a {
	color: #DAA520;
	text-decoration: underline;
}
</style>
	<?php
}
?>
<div class="avi-terms-wrap">
	<label class="avi-terms-label">
		<input type="checkbox"
			name="avi_accept_terms"
			id="avi_accept_terms"
			value="1"
			required />
		<span><?php echo wp_kses( $_tc_label, $_tc_kses ); ?></span>
	</label>
	<?php if ( $avi_terms_mode === 'nonprofit' ) : ?>
	<label class="avi-terms-label" style="margin-top:10px;">
		<input type="checkbox"
			name="avi_accept_ambassador"
			id="avi_accept_ambassador"
			value="1"
			required />
		<span><?php echo wp_kses( $_amb_label, $_tc_kses ); ?></span>
	</label>
	<?php endif; ?>
</div>
