<?php
/**
 * Invoice Row Partial
 *
 * Renders a single row in the invoice history table.
 *
 * @package FreeFacTu
 * @since   1.0.0
 * 
 * @var object   $freefactu_invoice      Invoice database object
 * @var WC_Order $order                  Order object (may be null if deleted)
 * @var bool     $freefactu_order_exists Whether the order still exists
 * @var string   $freefactu_qr_data_uri  Base64 QR code data URI (if generated)
 * @var string   $freefactu_qr_url       QR verification URL
 * @var string   $freefactu_qr_img       QR image HTML or flag
 */

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

// Get status
$freefactu_status        = isset( $freefactu_invoice->status ) ? $freefactu_invoice->status : 'success';
$freefactu_error_message = isset( $freefactu_invoice->error_message ) ? $freefactu_invoice->error_message : '';

// Status icon classes
$freefactu_status_icon  = '';
$freefactu_status_title = '';
if ( $freefactu_status === 'success' ) {
	$freefactu_status_icon  = '<span class="dashicons dashicons-yes-alt freefactu-status-icon--success" aria-hidden="true"></span>';
	$freefactu_status_title = __( 'Enviada correctamente', 'freefactu' );
} elseif ( $freefactu_status === 'alert_aeat' ) {
	$freefactu_status_icon  = '<span class="dashicons dashicons-warning freefactu-status-icon--warning" title="' . esc_attr( $freefactu_error_message ) . '" aria-hidden="true"></span>';
	$freefactu_status_title = __( 'Error al enviar a AEAT', 'freefactu' );
} elseif ( $freefactu_status === 'alert_qr' ) {
	$freefactu_status_icon  = '<span class="dashicons dashicons-warning freefactu-status-icon--warning" title="' . esc_attr( $freefactu_error_message ) . '" aria-hidden="true"></span>';
	$freefactu_status_title = __( 'Error al generar QR', 'freefactu' );
}

// Check badge for rectificativa
$freefactu_is_rectificativa = ! empty( $freefactu_invoice->tipo_factura ) && in_array( $freefactu_invoice->tipo_factura, array( 'R1', 'R2', 'R3', 'R4', 'R5' ), true );

// Status indicators
$freefactu_sent_to_aeat = ! empty( $freefactu_invoice->csv );

// QR cache check
$freefactu_qr_cached = false;
if ( ! empty( $freefactu_invoice->csv ) && ! empty( $freefactu_qr_url ) ) {
	$freefactu_cache_key = 'freefactu_qr_v2_' . md5( $freefactu_qr_url . 100 );
	$freefactu_qr_cached = get_transient( $freefactu_cache_key ) !== false;
}

// PDF status
$freefactu_absolute_pdf_path = Freefactu_Filesystem::to_absolute_path( $freefactu_invoice->pdf_path ?? '' );
$freefactu_pdf_created       = ! empty( $freefactu_invoice->pdf_path ) && Freefactu_Filesystem::exists( $freefactu_absolute_pdf_path );
?>

<tr data-order-id="<?php echo esc_attr( $freefactu_invoice->order_id ); ?>">
	<td>
		<?php if ( $freefactu_order_exists ) : ?>
			<a href="<?php echo esc_url( admin_url( 'post.php?post=' . $freefactu_invoice->order_id . '&action=edit' ) ); ?>">
				#<?php echo esc_html( $order->get_order_number() ); ?>
			</a>
		<?php else : ?>
			#<?php echo esc_html( $freefactu_invoice->order_id ); ?> 
			<span class="freefactu-deleted-order">(<?php esc_html_e( 'Eliminado', 'freefactu' ); ?>)</span>
		<?php endif; ?>
	</td>
	
	<td>
		<?php 
		echo esc_html( $freefactu_invoice->invoice_number );
		if ( $freefactu_is_rectificativa ) {
			echo ' <span class="freefactu-badge-rectificativa" title="' . esc_attr__( 'Factura rectificativa', 'freefactu' ) . '">R</span>';
		}
		?>
	</td>
	
	<td><?php echo esc_html( date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), strtotime( $freefactu_invoice->invoice_date ) ) ); ?></td>
	
	<td><?php echo wp_kses_post( wc_price( $freefactu_invoice->total ) ); ?></td>
	
	<td>
		<?php if ( $freefactu_qr_img === 'HAS_QR' && ! empty( $freefactu_qr_data_uri ) ) : ?>
			<a href="<?php echo esc_url( $freefactu_qr_url ); ?>" target="_blank">
				<img 
					src="<?php echo esc_attr( $freefactu_qr_data_uri ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Data URI validated by is_valid_data_uri(). esc_url() strips data: protocol. ?>" 
					alt="<?php esc_attr_e( 'QR Code', 'freefactu' ); ?>" 
					class="freefactu-qr-image" 
				/>
			</a>
		<?php else : ?>
			<?php echo wp_kses_post( $freefactu_qr_img ); ?>
		<?php endif; ?>
	</td>
	
	<td>
		<div class="freefactu-status-list">
			<div>
				<span class="<?php echo esc_attr( $freefactu_sent_to_aeat ? 'freefactu-status-check' : 'freefactu-status-cross' ); ?>">
				<?php echo $freefactu_sent_to_aeat ? '<span class="dashicons dashicons-yes-alt"></span>' : '<span class="dashicons dashicons-no-alt"></span>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Hardcoded HTML strings only. ?>
			</span>
				<?php esc_html_e( 'Enviado a AEAT', 'freefactu' ); ?>
			</div>
			<div>
				<span class="<?php echo esc_attr( $freefactu_qr_cached ? 'freefactu-status-check' : 'freefactu-status-cross' ); ?>">
				<?php echo $freefactu_qr_cached ? '<span class="dashicons dashicons-yes-alt"></span>' : '<span class="dashicons dashicons-no-alt"></span>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Hardcoded HTML strings only. ?>
			</span>
				<?php esc_html_e( 'QR en caché', 'freefactu' ); ?>
			</div>
			<div>
				<span class="<?php echo esc_attr( $freefactu_pdf_created ? 'freefactu-status-check' : 'freefactu-status-cross' ); ?>">
				<?php echo $freefactu_pdf_created ? '<span class="dashicons dashicons-yes-alt"></span>' : '<span class="dashicons dashicons-no-alt"></span>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Hardcoded HTML strings only. ?>
			</span>
				<?php esc_html_e( 'PDF creado', 'freefactu' ); ?>
			</div>
		</div>
	</td>
	
	<td>
		<?php
		// Centralized icon generation - functions escape all dynamic values and use only hardcoded HTML/SVG.
		// See docblock in class-freefactu-admin.php for security documentation.
		if ( $freefactu_order_exists ) {
			// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Function uses esc_attr/esc_url/esc_html for all dynamic values.
			echo Freefactu_Admin::render_verifactu_icon_html( $order, 'invoice_list' );
		} else {
			// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Function uses esc_attr/esc_url/esc_html for all dynamic values.
			echo Freefactu_Admin::render_verifactu_icon_html_from_invoice( $freefactu_invoice, 'invoice_list' );
		}
		?>
		<button type="button" 
			class="button button-small freefactu-debug-button freefactu-open-debug-modal"
			data-order-id="<?php echo esc_attr( $freefactu_invoice->order_id ); ?>"
			title="<?php esc_attr_e( 'Ver información de depuración', 'freefactu' ); ?>">
			<span class="dashicons dashicons-info" aria-hidden="true"></span>
		</button>
	</td>
</tr>
