<?php

namespace WarrantyCart\Frontend;

use WC_Product;

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

/**
 * Frontend Shortcode Class
 */
class Shortcode {

	/**
	 * Constructor
	 */
	public function __construct() {
		add_shortcode( 'warranty_cart', array( $this, 'render_shortcode' ) );
	}

	/**
	 * Render the warranty cart shortcode
	 *
	 * @param array<string, mixed> $atts Shortcode attributes.
	 *
	 * @return string
	 */
	public function render_shortcode( array $atts = array() ): string {
		/**
		 * Filter shortcode attributes
		 *
		 * @param array $atts Shortcode attributes.
		 */
		$atts = apply_filters( 'warranty_cart_shortcode_atts', $atts );

		$atts = shortcode_atts(
			array(
				'layout' => 'grid',
			),
			$atts
		);

		// Get warranty products
		$products = $this->get_warranty_products();

		if ( empty( $products ) ) {
			/**
			 * Filter empty products message
			 *
			 * @param string $message Empty products message.
			 */
			$message = apply_filters( 'warranty_cart_no_products_message', '<p>' . esc_html__( 'No warranty products found.', 'warranty-cart' ) . '</p>' );

			return $message;
		}

		$output = $this->render_product_selector( $products, $atts['layout'] );

		/**
		 * Filter shortcode output
		 *
		 * @param string $output   Shortcode output.
		 * @param array  $products Warranty products.
		 * @param array  $atts     Shortcode attributes.
		 */
		return apply_filters( 'warranty_cart_shortcode_output', $output, $products, $atts );
	}

	/**
	 * Get warranty products
	 *
	 * Retrieves enabled warranty products from the database.
	 *
	 * @return array Array of warranty product objects.
	 */
	private function get_warranty_products(): array {
		/**
		 * Filter warranty products query arguments
		 *
		 * @param array $args Query arguments.
		 */
		$args = apply_filters(
			'warranty_cart_shortcode_products_query',
			array(
				'limit'      => - 1,
				'meta_query' => array(
					array(
						'key'     => '_warranty_cart_product_status',
						'value'   => 'enabled',
						'compare' => '=',
					),
					array(
						'key'     => '_warranty_cart_product_type',
						'value'   => array( 'main_warranty', 'additional_service' ),
						'compare' => 'IN',
					),
				),
			)
		);

		$products = warranty_cart()->reports->get_warranty_products( $args );

		/**
		 * Filter warranty products for shortcode
		 *
		 * @param array $products Warranty products.
		 * @param array $args     Query arguments.
		 */
		return apply_filters( 'warranty_cart_shortcode_products', $products, $args );
	}

	/**
	 * Render product selector UI
	 *
	 * Generates the HTML for the warranty product selector interface.
	 *
	 * @param array  $products Products to display.
	 * @param string $layout   Layout type (grid or list).
	 *
	 * @return string HTML content for the product selector.
	 */
	private function render_product_selector( array $products, string $layout ): string {
		$main_warranties = array_filter(
			$products,
			static function ( $product ) {
				return $product instanceof WC_Product && $product->get_meta( '_warranty_product_type', true ) === 'main_warranty';
			}
		);

		$services = array_filter(
			$products,
			static function ( $product ) {
				return $product instanceof WC_Product && $product->get_meta( '_warranty_product_type', true ) === 'additional_service';
			}
		);

		return wc_get_template_html(
			'frontend/warranty-selector.php',
			array(
				'main_warranties' => $main_warranties,
				'services'        => $services,
				'layout'          => $layout,
			),
			'warranty-cart',
			WARRANTY_CART_PATH . 'templates/'
		);
	}
}
