<?php
/**
 * Orders table integration for invoice download column.
 *
 * @package PDFDraft_WooCommerce
 */

namespace PDFDraft\Woocommerce\Admin;

use PDFDraft\Woocommerce\Abstracts\AbstractIntegration;
use PDFDraft\Woocommerce\Core\Assets;

/**
 * Adds an invoice download action column to the WooCommerce orders list table.
 */
class OrdersTable extends AbstractIntegration {

	/**
	 * Assets service instance.
	 *
	 * @var Assets
	 */
	private Assets $assets;

	/**
	 * Initialize the OrdersTable integration.
	 *
	 * @param Assets $assets Assets service.
	 */
	public function __construct( Assets $assets ) {
		$this->assets = $assets;
	}

	/**
	 * Register WordPress hooks for the orders table.
	 *
	 * @return void
	 */
	public function initHooks() {
		if ( ! $this->hasActiveDesign() ) {
			return;
		}
		$this->addFilter( 'manage_woocommerce_page_wc-orders_columns', array( $this, 'addActionColumn' ) );
		$this->addAction( 'manage_woocommerce_page_wc-orders_custom_column', array( $this, 'renderInvoiceDownloadButton' ), 10, 2 );
		$this->addAction( 'load-woocommerce_page_wc-orders', array( $this, 'enqueueAssets' ) );
		// Same as load-* but runs on the canonical admin hook (avoids missed enqueues on some setups).
		$this->addAction( 'admin_enqueue_scripts', array( $this, 'enqueueAssetsOnWcOrdersScreen' ), 10, 1 );
	}

	/**
	 * Add the PDFDraft actions column to the orders table.
	 *
	 * @param array $columns Existing table columns.
	 * @return array Modified table columns.
	 */
	public function addActionColumn( $columns ) {
		$columns['pdfdraft-actions'] = __( 'Actions', 'pdfdraft-pdf-invoices-for-woocommerce' );
		return $columns;
	}

	/**
	 * Render the invoice download button for a given order row.
	 *
	 * @param string              $column Current column identifier.
	 * @param \WC_Order|int|mixed $order  The order object or order ID.
	 * @return void
	 */
	public function renderInvoiceDownloadButton( $column, $order ) {
		if ( 'pdfdraft-actions' !== $column ) {
			return;
		}

		$order_obj    = $order instanceof \WC_Order ? $order : wc_get_order( $order );
		$order_id     = $order_obj instanceof \WC_Order ? $order_obj->get_id() : (int) $order;
		$download_url = rest_url( 'pdfdraft-pdf-invoices-for-woocommerce/v1/orders/' . $order_id . '/invoice' );
		$download_url = apply_filters( 'pdfdraft_pdf_invoices_for_woocommerce_invoice_download_url', $download_url, $order_id );
		$args         = array(
			'order_id'     => $order_id,
			'context'      => 'orders-table',
			'type'         => 'icon',
			'size'         => 'small',
			'download_url' => $download_url,
		);
		if ( $order_obj instanceof \WC_Order ) {
			$args = apply_filters( 'pdfdraft_pdf_invoices_for_woocommerce_invoice_download_button_args', $args, $order_obj );
		}
		$this->renderTemplate( 'invoice-download-button.php', $args );
	}

	/**
	 * Enqueue invoice download scripts and styles for the orders table.
	 *
	 * @return void
	 */
	public function enqueueAssets() {
		$this->assets->enqueueInvoiceDownloadScriptStyle();
	}

	/**
	 * Enqueue on admin_enqueue_scripts for the HPOS orders screen (list + edit).
	 *
	 * @param string $hook_suffix Current admin page hook suffix.
	 * @return void
	 */
	public function enqueueAssetsOnWcOrdersScreen( string $hook_suffix = '' ): void {
		if ( 'woocommerce_page_wc-orders' !== $hook_suffix ) {
			return;
		}
		$this->enqueueAssets();
	}

	/**
	 * Check whether an active WooCommerce invoice design is configured.
	 *
	 * @return bool True if an active design slug is set.
	 */
	private function hasActiveDesign(): bool {
		$slug = get_option( 'pdfdraft_pdf_invoices_for_woocommerce_active_design_slug', '' );
		return is_string( $slug ) && '' !== $slug;
	}
}
