<?php

declare(strict_types=1);

namespace Signocore\Multilanguage\Infrastructure\Compatibility;

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

use Signocore\Multilanguage\Application\Constants;
use Signocore\Multilanguage\Features\LanguageDetection;
use Signocore\Multilanguage\Features\LanguageManager;
use Signocore\Multilanguage\Infrastructure\Traits\Singleton;
use Signocore\Multilanguage\Support\TranslationHelper;

final class WooCommerce
{
	use Singleton;

	protected function init(): void
	{
		if (!class_exists('WooCommerce')) {
			return;
		}

		// Make product post type translatable
		add_filter('signocore_ml_is_translatable_post_type', [$this, 'makeProductTranslatable'], 10, 2);

		// Make product taxonomies translatable
		add_filter('signocore_ml_is_translatable_taxonomy', [$this, 'makeProductTaxonomiesTranslatable'], 10, 2);

		// Filter WooCommerce product queries by language
		add_filter('woocommerce_product_query_tax_query', [$this, 'filterProductQuery']);

		// Translate cart item data
		add_filter('woocommerce_cart_item_product', [$this, 'translateCartItem'], 10, 3);

		// Translate WooCommerce page IDs
		add_filter('woocommerce_get_shop_page_id', [$this, 'translateShopPageId']);
		add_filter('woocommerce_get_cart_page_id', [$this, 'translateShopPageId']);
		add_filter('woocommerce_get_checkout_page_id', [$this, 'translateShopPageId']);
		add_filter('woocommerce_get_myaccount_page_id', [$this, 'translateShopPageId']);
		add_filter('woocommerce_get_terms_page_id', [$this, 'translateShopPageId']);

		// Add WooCommerce slug types for permalink translation
		add_filter('signocore_ml_translatable_permalink_slugs', [$this, 'addWooCommerceSlugs']);

		// Handle currency per language (future enhancement hook)
		do_action('signocore_ml_woocommerce_loaded');
	}

	/**
	 * Make 'product' post type always translatable.
	 */
	public function makeProductTranslatable(bool $translatable, string $postType): bool
	{
		if ($postType === 'product') {
			return true;
		}

		return $translatable;
	}

	/**
	 * Make WooCommerce taxonomies translatable.
	 */
	public function makeProductTaxonomiesTranslatable(bool $translatable, string $taxonomy): bool
	{
		$wooTaxonomies = ['product_cat', 'product_tag', 'product_shipping_class'];

		if (in_array($taxonomy, $wooTaxonomies, true)) {
			return true;
		}

		// Product attributes (pa_*)
		if (str_starts_with($taxonomy, 'pa_')) {
			return true;
		}

		return $translatable;
	}

	/**
	 * Filter product queries by language.
	 *
	 * @param array<int, array<string, mixed>> $taxQuery
	 * @return array<int, array<string, mixed>>
	 */
	public function filterProductQuery(array $taxQuery): array
	{
		// Language filtering is already handled by PostTranslation's pre_get_posts
		return $taxQuery;
	}

	/**
	 * Translate cart item product to current language.
	 *
	 * @param \WC_Product $product
	 * @param array<string, mixed> $cartItem
	 * @param string $cartItemKey
	 * @return \WC_Product
	 */
	public function translateCartItem($product, array $cartItem, string $cartItemKey)
	{
		$currentLang = LanguageDetection::load()->getCurrentLanguage();
		$productId = $product->get_id();

		$translatedId = TranslationHelper::getTranslation($productId, $currentLang);
		if ($translatedId !== null && $translatedId !== $productId) {
			$translatedProduct = wc_get_product($translatedId);
			if ($translatedProduct !== false) {
				return $translatedProduct;
			}
		}

		return $product;
	}

	/**
	 * Translate the shop page ID for the current language.
	 */
	public function translateShopPageId(int $pageId): int
	{
		$currentLang = LanguageDetection::load()->getCurrentLanguage();
		$translatedId = TranslationHelper::getTranslation($pageId, $currentLang);

		return $translatedId ?? $pageId;
	}

	/**
	 * Add WooCommerce-specific slug types for permalink translation.
	 *
	 * @param array<string, array{label: string, slug: string, pro: bool}> $slugs
	 * @return array<string, array{label: string, slug: string, pro: bool}>
	 */
	public function addWooCommerceSlugs(array $slugs): array
	{
		$productSlug = 'product';
		$ptObject = get_post_type_object('product');
		if ($ptObject !== null && isset($ptObject->rewrite['slug'])) {
			$productSlug = $ptObject->rewrite['slug'];
		}

		$slugs['post:product'] = [
			'label' => __('Product', 'signocore-multilanguage'),
			'slug' => $productSlug,
			'pro' => true,
		];

		$productCatTax = get_taxonomy('product_cat');
		$productCatSlug = ($productCatTax !== false && isset($productCatTax->rewrite['slug']))
			? $productCatTax->rewrite['slug']
			: 'product-category';

		$slugs['tax:product_cat'] = [
			'label' => __('Product Category', 'signocore-multilanguage'),
			'slug' => $productCatSlug,
			'pro' => true,
		];

		$productTagTax = get_taxonomy('product_tag');
		$productTagSlug = ($productTagTax !== false && isset($productTagTax->rewrite['slug']))
			? $productTagTax->rewrite['slug']
			: 'product-tag';

		$slugs['tax:product_tag'] = [
			'label' => __('Product Tag', 'signocore-multilanguage'),
			'slug' => $productTagSlug,
			'pro' => true,
		];

		return $slugs;
	}
}
