<?php
/**
 * Plugin Name: TeknoTok Sales Pages Helper
 * Description: Automatically serves and binds the TeknoTok AI Translator free and premium landing pages on soft.teknotok.com. Configure settings in Settings > Sales Pages Settings.
 * Version: 1.2.0
 * Author: TeknoTok Soft
 */

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

/**
 * Handle template redirect to intercept page loads and display our landing pages.
 */
function teknotok_serve_landing_pages() {
	global $post;

	if ( ! $post ) {
		return;
	}

	$pro_product_id = (int) get_option( 'teknotok_pro_product_id', '44' );
	$is_target_page = false;
	$file_path = '';

	// Intercept standard landing pages
	if ( is_page() ) {
		if ( $post->post_name === 'teknotok-translator-free' ) {
			$file_path = __DIR__ . '/free-product-page.html';
			$is_target_page = true;
		} elseif ( $post->post_name === 'teknotok-translator-pro' ) {
			$file_path = __DIR__ . '/premium-product-page.html';
			$is_target_page = true;
		}
	}

	// Intercept WooCommerce product matching the configured Pro Product ID
	if ( is_singular( 'product' ) && (int) $post->ID === $pro_product_id ) {
		$file_path = __DIR__ . '/premium-product-page.html';
		$is_target_page = true;
	}

	// Intercept WooCommerce product matching standard slugs as a fallback
	if ( is_singular( 'product' ) && ! $is_target_page ) {
		if ( in_array( $post->post_name, array( 'teknotok-translator-pro', 'teknotok-ai-translator-pro' ), true ) ) {
			$file_path = __DIR__ . '/premium-product-page.html';
			$is_target_page = true;
		}
	}

	if ( $is_target_page && ! empty( $file_path ) && file_exists( $file_path ) ) {
		$html = file_get_contents( $file_path );
		$html = teknotok_prepare_page_assets( $html );
		echo $html;
		exit;
	}
}
add_action( 'template_redirect', 'teknotok_serve_landing_pages' );

/**
 * Replace local relative paths with dynamic WordPress URLs.
 */
function teknotok_prepare_page_assets( $html ) {
	// Stylesheet URL
	$css_url = esc_url( plugins_url( 'styles.css', __FILE__ ) );
	$html = str_replace( 'href="styles.css"', 'href="' . $css_url . '"', $html );

	// Javascript URL
	$js_url = esc_url( plugins_url( 'main.js', __FILE__ ) );
	$html = str_replace( 'src="main.js"', 'src="' . $js_url . '"', $html );

	// Page cross-links
	$free_page_url = esc_url( home_url( '/teknotok-translator-free/' ) );
	$pro_page_url = esc_url( home_url( '/teknotok-translator-pro/' ) );
	
	$html = str_replace( 'href="free-product-page.html"', 'href="' . $free_page_url . '"', $html );
	$html = str_replace( 'href="premium-product-page.html"', 'href="' . $pro_page_url . '"', $html );

	// Resolve dynamic settings from Database options
	$pro_product_id = get_option( 'teknotok_pro_product_id', '44' );
	$checkout_url = esc_url( home_url( '/checkout/?add-to-cart=' . $pro_product_id ) );
	$html = str_replace( 'https://soft.teknotok.com/cart/?add-to-cart=PRO_PRODUCT_ID', $checkout_url, $html );

	$free_download_url = get_option( 'teknotok_free_download_url', 'https://soft.teknotok.com/download/teknotok-ai-translator.zip' );
	$html = str_replace( 'https://soft.teknotok.com/download/teknotok-ai-translator.zip', esc_url( $free_download_url ), $html );

	return $html;
}

/**
 * Register Admin Settings Page
 */
function teknotok_sales_helper_menu() {
	add_options_page(
		'Sales Pages Settings',
		'Sales Pages Settings',
		'manage_options',
		'teknotok-sales-helper',
		'teknotok_sales_helper_options_page'
	);
}
add_action( 'admin_menu', 'teknotok_sales_helper_menu' );

/**
 * Register settings variables
 */
function teknotok_sales_helper_init_settings() {
	register_setting( 'teknotok_sales_helper_group', 'teknotok_free_download_url' );
	register_setting( 'teknotok_sales_helper_group', 'teknotok_pro_product_id' );
}
add_action( 'admin_init', 'teknotok_sales_helper_init_settings' );

/**
 * Render options form
 */
function teknotok_sales_helper_options_page() {
	if ( ! current_user_can( 'manage_options' ) ) {
		return;
	}
	?>
	<div class="wrap">
		<h1>TeknoTok Sales Pages Helper Settings</h1>
		<p>Set custom options for the Free and Pro translator landing pages served on <code>/teknotok-translator-free/</code> and <code>/teknotok-translator-pro/</code>.</p>
		
		<form action="options.php" method="post">
			<?php
			settings_fields( 'teknotok_sales_helper_group' );
			do_settings_sections( 'teknotok_sales_helper_group' );
			?>
			<table class="form-table" role="presentation">
				<tr>
					<th scope="row">
						<label for="teknotok_free_download_url">Free Version Download URL</label>
					</th>
					<td>
						<input type="url" name="teknotok_free_download_url" id="teknotok_free_download_url" value="<?php echo esc_attr( get_option( 'teknotok_free_download_url', 'https://soft.teknotok.com/download/teknotok-ai-translator.zip' ) ); ?>" class="regular-text" placeholder="https://example.com/checkout/?add-to-cart=FREE_ID">
						<p class="description">Enter the direct link to download the free version file, or a WooCommerce cart checkout link for the free product.</p>
					</td>
				</tr>
				<tr>
					<th scope="row">
						<label for="teknotok_pro_product_id">Premium (Pro) Product ID</label>
					</th>
					<td>
						<input type="number" name="teknotok_pro_product_id" id="teknotok_pro_product_id" value="<?php echo esc_attr( get_option( 'teknotok_pro_product_id', '44' ) ); ?>" class="small-text" min="1">
						<p class="description">Enter the database ID of your Premium plugin in WooCommerce (e.g. <code>44</code>). The CTA buttons on the Pro page will link directly to `/checkout/?add-to-cart=YOUR_ID`.</p>
					</td>
				</tr>
			</table>
			<?php submit_button(); ?>
		</form>
	</div>
	<?php
}

/**
 * Pre-fill checkout license domain field from URL query parameter
 */
function teknotok_prefill_checkout_domain_field( $value, $input = null ) {
	if ( ! empty( $_GET['license_domain'] ) ) {
		return sanitize_text_field( wp_unslash( $_GET['license_domain'] ) );
	}
	return $value;
}
add_filter( 'woocommerce_checkout_get_value', 'teknotok_prefill_checkout_domain_field', 10, 2 );
add_filter( 'woocommerce_get_default_value_for_teknotok-license/license-domain', 'teknotok_prefill_checkout_domain_field', 10, 1 );

