<?php

namespace Devikit\Ifirma\Admin;

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

/**
 * Product fields preview (FREE without PRO installed)
 * Shows disabled GTU/PKWiU/Unit fields with a lock icon.
 */
class ProductFieldsPreview {

	public function __construct() {
		add_action( 'woocommerce_product_options_general_product_data', [ $this, 'add_product_fields_preview' ] );
		add_action( 'woocommerce_variation_options_pricing', [ $this, 'add_variation_fields_preview' ], 10, 3 );
	}

	/**
	 * Whether PRO plugin is installed (fields are rendered by PRO itself).
	 */
	private function is_pro_installed() {
		return class_exists( 'Devikit\IfirmaPro\Plugin' );
	}

	public function add_product_fields_preview() {
		if ( $this->is_pro_installed() ) {
			return;
		}

		echo '<div class="options_group ifirma-product-fields" style="opacity:0.6;">';
		echo '<h4 style="margin:15px 12px 5px;">' . esc_html__( 'iFirma Settings', 'invoicing-integration-for-ifirma-and-woocommerce' ) . ' 🔒</h4>';

		woocommerce_wp_select( [
			'id' => '_ifirma_gtu',
			'label' => __( 'GTU Code', 'invoicing-integration-for-ifirma-and-woocommerce' ),
			'desc_tip' => true,
			'description' => __( 'Available in PRO version.', 'invoicing-integration-for-ifirma-and-woocommerce' ),
			'options' => $this->get_gtu_options_preview(),
			'custom_attributes' => [ 'disabled' => 'disabled' ],
		] );

		woocommerce_wp_text_input( [
			'id' => '_ifirma_pkwiu',
			'label' => __( 'PKWiU Code', 'invoicing-integration-for-ifirma-and-woocommerce' ),
			'desc_tip' => true,
			'description' => __( 'Available in PRO version.', 'invoicing-integration-for-ifirma-and-woocommerce' ),
			'placeholder' => __( 'e.g., 85.59.11.0', 'invoicing-integration-for-ifirma-and-woocommerce' ),
			'custom_attributes' => [ 'disabled' => 'disabled' ],
		] );

		woocommerce_wp_text_input( [
			'id' => '_ifirma_unit',
			'label' => __( 'Unit of Measure', 'invoicing-integration-for-ifirma-and-woocommerce' ),
			'desc_tip' => true,
			'description' => __( 'Available in PRO version.', 'invoicing-integration-for-ifirma-and-woocommerce' ),
			'placeholder' => __( 'e.g., szt.', 'invoicing-integration-for-ifirma-and-woocommerce' ),
			'custom_attributes' => [ 'disabled' => 'disabled' ],
		] );

		echo '</div>';
	}

	public function add_variation_fields_preview( $loop, $variation_data, $variation ) {
		if ( $this->is_pro_installed() ) {
			return;
		}

		$variation_id = $variation->ID;

		echo '<div class="ifirma-variation-fields" style="border-top:1px solid #eee; padding-top:10px; margin-top:10px; opacity:0.6;">';
		echo '<h4 style="margin:0 0 10px;">' . esc_html__( 'iFirma Settings', 'invoicing-integration-for-ifirma-and-woocommerce' ) . ' 🔒</h4>';

		woocommerce_wp_select( [
			'id' => "_ifirma_gtu_{$variation_id}",
			'name' => "_ifirma_gtu[{$loop}]",
			'label' => __( 'GTU Code', 'invoicing-integration-for-ifirma-and-woocommerce' ),
			'desc_tip' => true,
			'description' => __( 'Available in PRO version.', 'invoicing-integration-for-ifirma-and-woocommerce' ),
			'options' => $this->get_gtu_options_preview(),
			'value' => '',
			'wrapper_class' => 'form-row form-row-first',
			'custom_attributes' => [ 'disabled' => 'disabled' ],
		] );

		woocommerce_wp_text_input( [
			'id' => "_ifirma_pkwiu_{$variation_id}",
			'name' => "_ifirma_pkwiu[{$loop}]",
			'label' => __( 'PKWiU Code', 'invoicing-integration-for-ifirma-and-woocommerce' ),
			'desc_tip' => true,
			'description' => __( 'Available in PRO version.', 'invoicing-integration-for-ifirma-and-woocommerce' ),
			'value' => '',
			'wrapper_class' => 'form-row form-row-last',
			'custom_attributes' => [ 'disabled' => 'disabled' ],
		] );

		echo '</div>';
	}

	private function get_gtu_options_preview() {
		$options = [
			'' => __( '— PRO —', 'invoicing-integration-for-ifirma-and-woocommerce' ),
		];

		for ( $i = 1; $i <= 13; $i++ ) {
			$key = 'GTU_' . str_pad( (string) $i, 2, '0', STR_PAD_LEFT );
			$options[ $key ] = $key;
		}

		return $options;
	}
}

