<?php
/**
 * Class Noesia Cs Product Passport Qr Module.
 *
 * @package Noesia_Commerce_Suite
 */

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

/**
 * Class implementation.
 */
class Noesia_CS_Product_Passport_QR_Module extends Noesia_CS_Admin_Module {
	/**
	 * Handles the related plugin operation.
	 */
	public function __construct() {
		$this->key         = 'product-passport-qr';
		$this->label       = __( 'Product Passport QR', 'noesia-commerce-suite' );
		$this->icon        = '▣';
		$this->description = __( 'Secure product passports, local QR codes and order care links.', 'noesia-commerce-suite' );
		$this->view        = 'modules/product-passport-qr/views/admin.php';
		$this->primary     = true;
	}


	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $context Context.
	 */
	public function prepare_data( $context = array() ) {
		$passport_service  = $this->service( 'passport_service' );
		$admin             = isset( $context['admin'] ) ? $context['admin'] : null;
		$orders            = $passport_service && method_exists( $passport_service, 'get_recent_orders' ) ? $passport_service->get_recent_orders( 12 ) : array();
		$rows              = array();

		foreach ( $orders as $order ) {
			if ( ! is_object( $order ) || ! method_exists( $order, 'get_id' ) ) {
				continue;
			}
			$order_id   = absint( $order->get_id() );
			$has_token  = $passport_service && method_exists( $passport_service, 'order_has_active_token' ) ? $passport_service->order_has_active_token( $order ) : false;
			$url        = ( $has_token && $passport_service && method_exists( $passport_service, 'get_existing_care_url_for_order' ) ) ? $passport_service->get_existing_care_url_for_order( $order ) : '';
			$rows[] = array(
				'order_id'        => $order_id,
				'reference'       => $passport_service && method_exists( $passport_service, 'get_safe_order_reference' ) ? $passport_service->get_safe_order_reference( $order ) : '#' . $order_id,
				'date'            => $order->get_date_created() ? $order->get_date_created()->date_i18n( get_option( 'date_format' ) ) : '',
				'customer_label'  => $admin && method_exists( $admin, 'safe_customer_label' ) ? $admin->safe_customer_label( $order ) : __( 'Customer', 'noesia-commerce-suite' ),
				'has_token'       => (bool) $has_token,
				'url'             => $url,
				'generate_url'    => wp_nonce_url( admin_url( 'admin-post.php?action=noesia_cs_generate_token&order_id=' . $order_id ), 'noesia_cs_generate_token_' . $order_id ),
				'print_url'       => $admin && method_exists( $admin, 'printable_qr_url' ) ? $admin->printable_qr_url( $order_id ) : '',
			);
		}

		return array(
			'orders'         => $orders,
			'passport_rows'  => $rows,
			'qr_settings'    => array(
				'size'           => absint( $this->setting( 'qr_size', 160 ) ),
				'foreground'     => sanitize_hex_color( $this->setting( 'qr_foreground_color', '#111827' ) ) ?: '#111827',
				'background'     => sanitize_hex_color( $this->setting( 'qr_background_color', '#ffffff' ) ) ?: '#ffffff',
				'enabled'        => 'yes' === $this->setting( 'enable_product_passport_qr', 'yes' ),
				'printable'      => 'yes' === $this->setting( 'allow_printable_qr', 'no' ),
			),
		);
	}

}
