<?php

declare(strict_types=1);

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

class KLCNN_Product_Fields
{
  public function __construct()
  {
    // Add EAN field to product general tab
    add_action('woocommerce_product_options_general_product_data', [$this, 'add_ean_field']);
    // Save EAN field value
    add_action('woocommerce_process_product_meta', [$this, 'save_ean_field']);
  }

  public function add_ean_field(): void
  {
    woocommerce_wp_text_input([
      'id' => '_klcnn_ean',
      'label' => __('EAN', 'easyconnect-for-kaufland'),
      'desc_tip' => true,
      'description' => __('Enter the European Article Number (EAN) for this product.', 'easyconnect-for-kaufland'),
      'type' => 'text'
    ]);
  }

  public function save_ean_field($post_id): void
  {
    $ean = isset($_POST['_klcnn_ean']) ? sanitize_text_field($_POST['_klcnn_ean']) : '';
    update_post_meta($post_id, '_klcnn_ean', $ean);
  }
}
