<?php

namespace WeDevs\AppBridge\Core\REST;

use WP_REST_Controller;
use WP_REST_Server;

/**
 * Generic helper endpoints (product / category / tag pickers) shared by every
 * mobile-app integration. Registered once at the core level under the common
 * `app-bridge/v1` namespace so admin UIs (sliders, link pickers) can resolve
 * the same data regardless of which integration is active.
 */
class HelperController extends WP_REST_Controller {

	/**
	 * Endpoint namespace.
	 *
	 * @var string
	 */
	protected $namespace = 'app-bridge/v1';

	/**
	 * Route base.
	 *
	 * @var string
	 */
	protected $rest_base = 'helpers';

	public function register_routes() {
		register_rest_route(
			$this->namespace, '/' . $this->rest_base . '/product-categories', [
				[
					'methods'             => WP_REST_Server::READABLE,
					'callback'            => [ $this, 'get_product_categories' ],
					'permission_callback' => [ $this, 'general_permission_callback' ],
				],
			]
		);

		register_rest_route(
			$this->namespace, '/' . $this->rest_base . '/product-tags', [
				[
					'methods'             => WP_REST_Server::READABLE,
					'callback'            => [ $this, 'get_product_tags' ],
					'permission_callback' => [ $this, 'general_permission_callback' ],
				],
			]
		);

		register_rest_route(
			$this->namespace, '/' . $this->rest_base . '/products', [
				[
					'methods'             => WP_REST_Server::READABLE,
					'callback'            => [ $this, 'get_products' ],
					'permission_callback' => [ $this, 'general_permission_callback' ],
				],
			]
		);
	}

	public function general_permission_callback() {
		return current_user_can( 'manage_options' );
	}

	private function get_terms( $taxonomy ) {
		$data = [];

		$terms = get_terms(
			[
				'taxonomy'   => $taxonomy,
				'hide_empty' => false,
			]
		);

		if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
			foreach ( $terms as $term ) {
				$data[] = [
					'id'        => $term->term_id,
					'slug'      => $term->slug,
					'name'      => $term->name,
					'parent_id' => $term->parent,
				];
			}
		}

		return $data;
	}

	public function get_product_categories() {
		$categories = $this->get_terms( 'product_cat' );

		$response = rest_ensure_response( $categories );
		$response->header( 'X-WP-TotalProductCategories', count( $categories ) );

		return $response;
	}

	public function get_product_tags() {
		$tags = $this->get_terms( 'product_tag' );

		$response = rest_ensure_response( $tags );
		$response->header( 'X-WP-TotalProductTags', count( $tags ) );

		return $response;
	}

	public function get_products() {
		$products = [];

		if ( ! function_exists( 'wc_get_products' ) ) {
			return rest_ensure_response( $products );
		}

		$wc_products = wc_get_products(
			[
				'limit'   => -1,
				'order'   => 'asc',
				'orderby' => 'post_title',
			]
		);

		if ( ! empty( $wc_products ) ) {
			foreach ( $wc_products as $product ) {
				$image_src = wp_get_attachment_image_src( $product->get_image_id(), 'woocommerce_thumbnail' );

				$products[] = [
					'id'        => $product->get_id(),
					'name'      => $product->get_name(),
					'slug'      => $product->get_slug(),
					'image'     => $image_src ? $image_src[0] : WC()->plugin_url() . '/assets/images/placeholder.png',
					'parent_id' => $product->get_parent_id(),
				];
			}
		}

		$response = rest_ensure_response( $products );
		$response->header( 'X-WP-TotalProducts', count( $products ) );

		return $response;
	}
}
