<?php
/**
 * PostGate Template Utilities Class
 *
 * Helper functions for template rendering and common operations.
 *
 * @package PostGate
 * @subpackage Helpers
 * @since 1.0.1
 * @author QodeSphere
 * @license GPL-2.0+
 */

defined( 'ABSPATH' ) || exit;

/**
 * PostGate Template Utilities Class
 *
 * Provides helper methods for template rendering to avoid code duplication
 * and centralize common template operations.
 *
 * @since 1.0.1
 */
class PostGate_Template_Utils {

	/**
	 * Get all paywall template settings from WordPress options.
	 *
	 * Centralizes the fetching of paywall-related options to avoid duplication
	 * across template files. Returns an associative array of all settings
	 * with their default values.
	 *
	 * @since 1.0.1
	 *
	 * @return array Associative array of paywall settings with keys:
	 *               - badge: Badge text
	 *               - title: Paywall title
	 *               - description: Paywall description
	 *               - content_before: Content before price options
	 *               - content_after: Content after price options
	 *               - button_text: Unlock button text
	 *               - login_text: Login link text
	 */
	public static function get_paywall_settings(): array {
		return [
			'badge' => get_option( 'postgate_paywall_badge', '' ),
			'title' => get_option( 'postgate_paywall_title', __( 'Choose an option to continue reading:', 'postgate' ) ),
			'description' => get_option( 'postgate_paywall_description', '' ),
			'content_before' => get_option( 'postgate_paywall_content_before', '' ),
			'content_after' => get_option( 'postgate_paywall_content_after', '' ),
			'button_text' => get_option( 'postgate_paywall_button_text', __( 'Unlock Content', 'postgate' ) ),
			'login_text' => get_option( 'postgate_paywall_login_text', __( 'Already purchased? Log in', 'postgate' ) ),
		];
	}

	/**
	 * Get all login gate template settings from WordPress options.
	 *
	 * Centralizes the fetching of login-related options to avoid duplication
	 * across template files.
	 *
	 * @since 1.0.1
	 *
	 * @return array Associative array of login gate settings with keys:
	 *               - badge: Badge text
	 *               - title: Login gate title
	 *               - description: Login gate description
	 *               - button_text: Login button text
	 *               - register_text: Register link text
	 *               - forgot_text: Forgot password link text
	 */
	public static function get_login_gate_settings(): array {
		return [
			'badge' => get_option( 'postgate_login_badge', '' ),
			'title' => get_option( 'postgate_login_title', __( 'This content requires login', 'postgate' ) ),
			'description' => get_option( 'postgate_login_description', __( 'This content is protected and meant for logged and/or registered users only.', 'postgate' ) ),
			'button_text' => get_option( 'postgate_login_button_text', __( 'Log in to continue', 'postgate' ) ),
			'register_text' => get_option( 'postgate_login_register_text', __( 'Create an account', 'postgate' ) ),
			'forgot_text' => get_option( 'postgate_login_forgot_text', __( 'Forgot password?', 'postgate' ) ),
		];
	}

	/**
	 * Get archive lock indicator settings.
	 *
	 * @since 1.0.1
	 *
	 * @return array Associative array of archive lock settings with keys:
	 *               - title: Archive lock title
	 *               - button_text: Button text
	 */
	public static function get_archive_lock_settings(): array {
		return [
			'title' => get_option( 'postgate_archive_lock_title', __( 'This content is protected', 'postgate' ) ),
			'button_text' => get_option( 'postgate_archive_lock_button_text', __( 'View post to unlock', 'postgate' ) ),
		];
	}

	/**
	 * Render a template partial.
	 *
	 * Loads and renders a template part file with the provided variables.
	 * Supports theme overrides - themes can override partials by placing them in
	 * 'postgate/parts/' directory in the theme.
	 *
	 * @since 1.0.1
	 *
	 * @param string $partial_name Name of the partial file (relative to templates directory).
	 *                             Example: 'parts/paywall-header'
	 * @param array  $vars         Variables to extract and make available to the template.
	 *
	 * @return void
	 */
	public static function partial( string $partial_name, array $vars = [] ): void {
		// Construct theme-friendly path (e.g., 'postgate/parts/paywall-header.php')
		$theme_path = 'postgate/' . $partial_name . '.php';

		// Check for theme override first
		$theme_template = locate_template( [ $theme_path ], false, false );

		if ( $theme_template ) {
			$partial_file = $theme_template;
		} else {
			// Fall back to plugin template
			$partial_file = POSTGATE_PATH . 'templates/' . $partial_name . '.php';
		}

		if ( ! file_exists( $partial_file ) ) {
			return;
		}

		// Extract variables for template use
		if ( ! empty( $vars ) ) {
			extract( $vars, EXTR_SKIP );
		}

		include $partial_file;
	}
}
