<?php

namespace HulkPlugins\Woo\ProductDescriptionInLoop\Frontend\Controllers;

use HulkPlugins\Woo\ProductDescriptionInLoop\Admin\Services\SettingsService;
use HulkPlugins\Woo\ProductDescriptionInLoop\Core\BaseController;
use HulkPlugins\Woo\ProductDescriptionInLoop\Core\SingletonTrait;
use HulkPlugins\Woo\ProductDescriptionInLoop\Frontend\Services\ArchivePagesService;
use HulkPlugins\Woo\ProductDescriptionInLoop\Frontend\Services\DescriptionService;
use HulkPlugins\Woo\ProductDescriptionInLoop\Support\SettingsConst;

defined( 'ABSPATH' ) || exit;

class ArchivePagesController extends BaseController {

	use SingletonTrait;

	public function init_hooks(): void {
		add_filter( 'render_block', [ $this, 'inject_into_block' ], 10, 2 );
	}

	/**
	 * Filters the content of a single block.
	 * Inject description on block pages
	 */
	public function inject_into_block( $block_content, $block ): string {

		$archiveService = ArchivePagesService::getInstance();

		// Exit early if not archive pages
		if ( ! $archiveService->isAllArchivePages() ) {
			return $block_content;
		}

		$settings = SettingsService::getInstance();
		$descriptionService = DescriptionService::getInstance();

		$enabledPages = $settings->getEnabledPages();

		$archiveEnabled = in_array( SettingsConst::ARCHIVE_PAGES, $enabledPages, true );

		// Detect current page type
		$isShop     = $archiveService->isShopPage();
		$isCategory = $archiveService->isCategoryPages();
		$isTag      = $archiveService->isTagPages();

		if ( $archiveEnabled ) {
			// ARCHIVE_PAGES acts as a global switch for all archive types
			if ( ! ( $isShop || $isCategory || $isTag ) ) {
				return $block_content;
			}
		} elseif (
			// No global archive → check individually
			( $isShop && ! in_array( SettingsConst::SHOP_PAGE, $enabledPages, true ) ) ||
			( $isCategory && ! in_array( SettingsConst::CATEGORY_PAGES, $enabledPages, true ) ) ||
			( $isTag && ! in_array( SettingsConst::TAG_PAGES, $enabledPages, true ) )
		) {
			return $block_content;
		}

		$descriptionPosition = $settings->getDescriptionPosition();
		$descriptionContent  = $settings->getDescriptionContent();
		$wordLimit           = $settings->getWordLimit();

		$blockName = $block['blockName'] ?? '';

		// Only proceed for relevant blocks
		$allowedBlocks = [
			SettingsConst::BLOCK_PRODUCT_TITLE,
			SettingsConst::BLOCK_PRODUCT_RATING,
			SettingsConst::BLOCK_PRODUCT_PRICE,
			SettingsConst::BLOCK_PRODUCT_BUTTON,
		];

		if ( ! in_array( $blockName, $allowedBlocks, true ) ) {
			return $block_content;
		}

		$description = $descriptionService->getDescription( $descriptionContent, $wordLimit );
		$description = $this->styled_description( $description );

		// Map position → block
		$beforeMap = [
			SettingsConst::BEFORE_PRODUCT_TITLE  => SettingsConst::BLOCK_PRODUCT_TITLE,
			SettingsConst::BEFORE_PRODUCT_RATING => SettingsConst::BLOCK_PRODUCT_RATING,
			SettingsConst::BEFORE_PRODUCT_PRICE  => SettingsConst::BLOCK_PRODUCT_PRICE,
			SettingsConst::BEFORE_ADD_TO_CART    => SettingsConst::BLOCK_PRODUCT_BUTTON,
		];

		// Handle position: prepend description before
		if (
			isset( $beforeMap[ $descriptionPosition ] ) &&
			$blockName === $beforeMap[ $descriptionPosition ]
		) {
			return wp_kses_post( $description ) . $block_content;
		}

		// Map position → block
		$afterMap = [
			SettingsConst::AFTER_PRODUCT_TITLE  => SettingsConst::BLOCK_PRODUCT_TITLE,
			SettingsConst::AFTER_PRODUCT_RATING => SettingsConst::BLOCK_PRODUCT_RATING,
			SettingsConst::AFTER_PRODUCT_PRICE  => SettingsConst::BLOCK_PRODUCT_PRICE,
			SettingsConst::AFTER_ADD_TO_CART    => SettingsConst::BLOCK_PRODUCT_BUTTON,
		];

		// Handle position: append description after
		if (
			isset( $afterMap[ $descriptionPosition ] ) &&
			$blockName === $afterMap[ $descriptionPosition ]
		) {
			return $block_content . wp_kses_post( $description );
		}

		return $block_content;
	}

	/**
	 * Description with style
	 */
	public function styled_description( string $text ): string {
		$settings = SettingsService::getInstance();
		$isEnableStyle = $settings->isEnableStyle();

		if ( $isEnableStyle ) {
			$archiveService = ArchivePagesService::getInstance();
			$textAlign = $settings->getTextAlign();
			$fontSize = $settings->getFontSize();
			$textColor = $settings->getTextColor();

			$style = '.hulk_woo_pdil_description {';

			if ( $textAlign ) {
				$style .= sprintf( 'text-align:%s;', $textAlign );
			}

			if ( $fontSize ) {
				$style .= sprintf( 'font-size:%spx;', $fontSize );
			}

			if ( $textColor ) {
				$style .= sprintf( 'color:%s;', $textColor );
			}

			$style .= '}';
			$archiveService->addInlineStyle( $style );
		}

		$markup = /** @lang text */ '<div class="hulk_woo_pdil_description">';
		$markup .= $text;
		$markup .= /** @lang text */ '</div>';
		return $markup;
	}
}
