<?php
/**
 * Customer Downloads Widget - Render Methods
 *
 * @package GGM_Commerce
 */

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

/**
 * Render the premium widget content.
 *
 * @param \GGM_Commerce_Widget_Customer_Downloads $widget The widget instance.
 */
function ggmc_customer_downloads_render_premium_content( $widget ) {
	$settings = $widget->get_settings_for_display();

	// Check if user is logged in.
	if ( ! is_user_logged_in() ) {
		ggmc_customer_downloads_render_login_message( $settings );
		return;
	}

	// Check if downloads class exists.
	if ( ! class_exists( 'GGM_Commerce_Downloads' ) ) {
		echo '<div class="ggmc-downloads-notice">';
		esc_html_e( 'Downloads feature is not available.', 'ggm-commerce' );
		echo '</div>';
		return;
	}

	$downloads_manager = GGM_Commerce_Downloads::instance();
	$user              = wp_get_current_user();
	$downloads         = $downloads_manager->get_customer_downloads( $user->user_email );

	?>
	<div class="ggmc-customer-downloads">
		<?php if ( 'yes' === $settings['show_title'] && ! empty( $settings['title'] ) ) : ?>
			<h3 class="ggmc-downloads-title"><?php echo esc_html( $settings['title'] ); ?></h3>
		<?php endif; ?>

		<?php if ( empty( $downloads ) ) : ?>
			<div class="ggmc-downloads-empty">
				<?php echo esc_html( $settings['empty_message'] ); ?>
			</div>
		<?php else : ?>
			<div class="ggmc-downloads-list">
				<?php foreach ( $downloads as $download ) : ?>
					<?php ggmc_customer_downloads_render_download_item( $download, $settings ); ?>
				<?php endforeach; ?>
			</div>
		<?php endif; ?>
	</div>
	<?php
}

/**
 * Render login required message.
 *
 * @param array $settings Widget settings.
 */
function ggmc_customer_downloads_render_login_message( $settings ) {
	?>
	<div class="ggmc-downloads-login-required">
		<p><?php echo esc_html( $settings['login_message'] ); ?></p>
		<?php if ( ! ggmc_is_editor_mode() ) : ?>
			<a href="<?php echo esc_url( wp_login_url( get_permalink() ) ); ?>" class="ggmc-login-link">
				<?php esc_html_e( 'Log In', 'ggm-commerce' ); ?>
			</a>
		<?php endif; ?>
	</div>
	<?php
}

/**
 * Render a single download item.
 *
 * @param object $download Download permission data.
 * @param array  $settings Widget settings.
 */
function ggmc_customer_downloads_render_download_item( $download, $settings ) {
	// Get the download file details.
	$downloads_manager = GGM_Commerce_Downloads::instance();
	$can_download      = $downloads_manager->can_download( $download->id, wp_get_current_user()->user_email );

	// Determine status.
	$is_expired       = ! empty( $download->access_expires_at ) && strtotime( $download->access_expires_at ) < time();
	$is_limit_reached = $download->download_limit > 0 && $download->download_count >= $download->download_limit;
	$is_available     = $can_download && ! $is_expired && ! $is_limit_reached;

	// Get download URL if available.
	$download_url = $is_available ? $downloads_manager->generate_download_url( $download->id ) : '';

	?>
	<div class="ggmc-download-item <?php echo ! $is_available ? 'unavailable' : ''; ?>">
		<div class="ggmc-download-info">
			<?php if ( 'yes' === $settings['show_product_name'] ) : ?>
				<div class="ggmc-download-product">
					<span class="ggmc-download-label"><?php esc_html_e( 'Product:', 'ggm-commerce' ); ?></span>
					<span class="ggmc-download-value"><?php echo esc_html( $download->product_name ?? __( 'Unknown Product', 'ggm-commerce' ) ); ?></span>
				</div>
			<?php endif; ?>

			<div class="ggmc-download-file">
				<span class="ggmc-download-label"><?php esc_html_e( 'File:', 'ggm-commerce' ); ?></span>
				<span class="ggmc-download-value"><?php echo esc_html( $download->download_name ?? __( 'Download', 'ggm-commerce' ) ); ?></span>
			</div>

			<?php if ( 'yes' === $settings['show_order_number'] && ! empty( $download->order_id ) ) : ?>
				<div class="ggmc-download-order">
					<span class="ggmc-download-label"><?php esc_html_e( 'Order:', 'ggm-commerce' ); ?></span>
					<span class="ggmc-download-value">#<?php echo esc_html( $download->order_id ); ?></span>
				</div>
			<?php endif; ?>

			<?php if ( 'yes' === $settings['show_download_limit'] && $download->download_limit > 0 ) : ?>
				<div class="ggmc-download-remaining">
					<span class="ggmc-download-label"><?php esc_html_e( 'Downloads:', 'ggm-commerce' ); ?></span>
					<span class="ggmc-download-value">
						<?php
						$remaining = max( 0, $download->download_limit - $download->download_count );
						/* translators: %d: remaining downloads count */
						echo esc_html( sprintf( __( '%d remaining', 'ggm-commerce' ), $remaining ) );
						?>
					</span>
				</div>
			<?php endif; ?>

			<?php if ( 'yes' === $settings['show_expiry_date'] && ! empty( $download->access_expires_at ) ) : ?>
				<div class="ggmc-download-expiry">
					<span class="ggmc-download-label"><?php esc_html_e( 'Expires:', 'ggm-commerce' ); ?></span>
					<span class="ggmc-download-value">
						<?php
						$expiry_date = strtotime( $download->access_expires_at );
						echo esc_html( date_i18n( get_option( 'date_format' ), $expiry_date ) );
						?>
					</span>
				</div>
			<?php endif; ?>
		</div>

		<div class="ggmc-download-actions">
			<?php if ( $is_expired ) : ?>
				<span class="ggmc-download-status expired">
					<?php esc_html_e( 'Expired', 'ggm-commerce' ); ?>
				</span>
			<?php elseif ( $is_limit_reached ) : ?>
				<span class="ggmc-download-status limit-reached">
					<?php esc_html_e( 'Download limit reached', 'ggm-commerce' ); ?>
				</span>
			<?php elseif ( $is_available && $download_url ) : ?>
				<a href="<?php echo esc_url( $download_url ); ?>"
					class="ggmc-download-button"
					target="_blank"
					rel="noopener noreferrer">
					<?php echo esc_html( $settings['download_button_text'] ); ?>
				</a>
			<?php else : ?>
				<span class="ggmc-download-status unavailable">
					<?php esc_html_e( 'Unavailable', 'ggm-commerce' ); ?>
				</span>
			<?php endif; ?>
		</div>
	</div>
	<?php
}
