<?php
/**
 * GeneralFields.php
 *
 * @since      1.0.0
 * @package    WcElectronInvoice\WooCommerce\Fields
 * @author     alfiopiccione <alfio.piccione@gmail.com>
 * @license    http://opensource.org/licenses/gpl-2.0.php GPL v2
 *
 * Copyright (C) 2025 POP
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

namespace WcElectronInvoice\WooCommerce\Fields;

use WcElectronInvoice\Admin\Settings\OptionPage;
use WcElectronInvoice\Utils\Countries;
use function WcElectronInvoice\Functions\sanMarinoSupport;

if (! defined('ABSPATH')) {
    exit; // Exit if accessed directly.
}

/**
 * Class GeneralFields
 *
 * @since  1.0.0
 * @author alfiopiccione <alfio.piccione@gmail.com>
 */
class GeneralFields
{
    /**
     * Regex VAT Code
     *
     * @since 1.0.0
     *
     * @var string
     */
    public $regexVAT = "/^(ATU[0-9]{8}|BE0[0-9]{9}|BG[0-9]{9,10}|CY[0-9]{8}L|CZ[0-9]{8,10}|DE[0-9]{9}|DK[0-9]{8}|EE[0-9]{9}|(EL|GR)[0-9]{9}|ES[0-9A-Z][0-9]{7}[0-9A-Z]|FI[0-9]{8}|FR[0-9A-Z]{2}[0-9]{9}|GB([0-9]{9}([0-9]{3})?|[A-Z]{2}[0-9]{13})|HU[0-9]{8}|IE[0-9][A-Z0-9][0-9]{5}[A-Z]{1,2}|IT[0-9]{11}|LT([0-9]{9}|[0-9]{12})|LU[0-9]{8}|LV[0-9]{11}|MT[0-9]{8}|NL[0-9]{9}B[0-9]{2}|PL[0-9]{10}|PT[0-9]{9}|RO[0-9]{2,10}|SE[0-9]{12}|SI[0-9]{8}|SK[0-9]{10})$/i";

    /**
     * General Fields
     *
     * @since 1.0.0
     *
     * @var array
     */
    private $fields;

    /**
     * Prefix option
     *
     * @since 1.0.0
     *
     * @var string
     */
    public static $optionPrefix = 'wc_el_inv-';

    /**
     * GeneralFields constructor.
     *
     * @param array $fields
     */
    public function __construct(array $fields)
    {
        $this->fields = $fields;
    }

    /**
     * Redirect to general tab
     *
     * necessary to check the fields
     */
    public function wcGeneralRedirect()
    {
        if (is_admin()) {
            $tab  = \WcElectronInvoice\Functions\filterInput($_GET, 'tab', FILTER_UNSAFE_RAW);
            $page = \WcElectronInvoice\Functions\filterInput($_GET, 'page', FILTER_UNSAFE_RAW);

            if ('wc-settings' === $page && false === $tab) {
                wp_redirect(admin_url('admin.php/?page=wc-settings&tab=general'));
                die();
            }
        }
    }

    /**
     * Get EU countries
     *
     * @return string[]
     */
    public static function getEuCountries()
    {
        $countries = new Countries();

        return $countries->get_european_union_countries();
    }

    /**
     * Add General invoice fields
     *
     * @param $fields
     *
     * @return array
     * @since 1.0.0
     *
     */
    public function generalInvoiceFields($fields)
    {
        if (in_array(self::getGeneralInvoiceOptionCountryState(), self::getEuCountries())) {
            $fields = array_merge($this->fields, $fields);
        }

        return $fields;
    }

    /**
     * General store name
     *
     * @return mixed Value set for the option.
     * @since 1.0.0
     *
     */
    public static function getGeneralInvoiceOptionName()
    {
        if (! in_array(self::getGeneralInvoiceOptionCountryState(), self::getEuCountries())) {
            return '';
        }

        return apply_filters('wc_el_inv-general_store_your_name',
            get_option(self::$optionPrefix . 'general_store_your_name')
        ) ?: '';
    }

    /**
     * General store surname
     *
     * @return mixed Value set for the option.
     * @since 1.0.0
     *
     */
    public static function getGeneralInvoiceOptionSurname()
    {
        if (! in_array(self::getGeneralInvoiceOptionCountryState(), self::getEuCountries())) {
            return '';
        }

        return apply_filters('wc_el_inv-general_store_your_surname',
            get_option(self::$optionPrefix . 'general_store_your_surname')
        ) ?: '';
    }

    /**
     * General store company name
     *
     * @return mixed Value set for the option.
     * @since 1.0.0
     *
     */
    public static function getGeneralInvoiceOptionCompanyName()
    {
        if (! in_array(self::getGeneralInvoiceOptionCountryState(), self::getEuCountries())) {
            return '';
        }

        $companyName = get_option(self::$optionPrefix . 'general_store_company_name');

        return apply_filters('wc_el_inv-general_store_company_name',
            \WcElectronInvoice\Functions\plainTextFromHtml($companyName, true)
        ) ?: '';
    }

    /**
     * General store vat number
     *
     * @return mixed Value set for the option.
     * @since 1.0.0
     *
     */
    public static function getGeneralInvoiceOptionVatNumber()
    {
        $sanMarinoSupport = sanMarinoSupport();
        if ('SM' !== $sanMarinoSupport->shopCountry &&
            ! in_array(self::getGeneralInvoiceOptionCountryState(), self::getEuCountries())
        ) {
            return '';
        }

        return apply_filters('wc_el_inv-general_store_vat_number',
            get_option(self::$optionPrefix . 'general_store_vat_number')
        ) ?: '';
    }

    /**
     * General store email address
     *
     * @return mixed Value set for the option.
     * @since 1.0.0
     *
     */
    public static function getGeneralInvoiceOptionEmailAddress()
    {
        if (! in_array(self::getGeneralInvoiceOptionCountryState(), self::getEuCountries())) {
            return '';
        }

        $generalEmail    = get_option(self::$optionPrefix . 'general_store_email');
        $accountingEmail = get_option(self::$optionPrefix . 'company_accounting_email');

        if (! $generalEmail && $accountingEmail) {
            update_option(self::$optionPrefix . 'general_store_email', $accountingEmail);
        }

        $mail = $accountingEmail ?: $generalEmail;

        return apply_filters('wc_el_inv-general_store_email', $mail) ?: '';
    }

    /**
     * General store phone number
     *
     * @return mixed Value set for the option.
     * @since 1.0.0
     *
     */
    public static function getGeneralInvoiceOptionPhoneNumber()
    {
        if (! in_array(self::getGeneralInvoiceOptionCountryState(), self::getEuCountries())) {
            return '';
        }

        return apply_filters('wc_el_inv-general_store_phone',
            get_option(self::$optionPrefix . 'general_store_phone')
        ) ?: '';
    }

    /**
     * General store tax regime
     *
     * @return mixed Value set for the option.
     * @since 1.0.0
     *
     */
    public static function getGeneralInvoiceOptionTaxRegime()
    {
        if (! in_array(self::getGeneralInvoiceOptionCountryState(), self::getEuCountries())) {
            return '';
        }

        return apply_filters('wc_el_inv-general_store_tax_regime',
            get_option(self::$optionPrefix . 'general_store_tax_regime')
        ) ?: '';
    }

    /**
     * General store office register business
     *
     * @return mixed Value set for the option.
     * @since 2.2.1
     *
     */
    public static function getGeneralInvoiceOptionProvinceOfficeBusinessRegister()
    {
        if (! in_array(self::getGeneralInvoiceOptionCountryState(), self::getEuCountries())) {
            return '';
        }

        return apply_filters('wc_el_inv-province_business_register_office',
            get_option(self::$optionPrefix . 'province_business_register_office')
        ) ?: '';
    }

    /**
     * General store REA number
     *
     * @return mixed Value set for the option.
     * @since 2.2.1
     *
     */
    public static function getGeneralInvoiceOptionReaNumber()
    {
        if (! in_array(self::getGeneralInvoiceOptionCountryState(), self::getEuCountries())) {
            return '';
        }

        return apply_filters('wc_el_inv-rea_registration_number',
            get_option(self::$optionPrefix . 'rea_registration_number')
        ) ?: '';
    }

    /**
     * General store liquidation status
     *
     * @return mixed Value set for the option.
     * @since 2.2.1
     *
     */
    public static function getGeneralInvoiceOptionLiquidationStatus()
    {
        if (! in_array(self::getGeneralInvoiceOptionCountryState(), self::getEuCountries())) {
            return '';
        }

        return apply_filters('wc_el_inv-liquidation_status',
            get_option(self::$optionPrefix . 'liquidation_status')
        ) ?: '';
    }

    /**
     * General store invoice country state
     *
     * @return mixed Value set for the option.
     * @since 1.0.0
     *
     */
    public static function getGeneralInvoiceOptionCountryState()
    {
        // Default
        $shopCountry = 'IT';

        // Configuration store country
        $shopCountry = get_option(self::$optionPrefix . 'general_store_country', $shopCountry);

        // San Marino support
        $sanMarinoSupport = sanMarinoSupport();
        if ('SM' === $sanMarinoSupport->shopCountry) {
            return 'SM';
        }

        if (! in_array($shopCountry, self::getEuCountries())) {
            return '';
        }

        return apply_filters('wc_el_inv-woocommerce_store_country_state',
            $shopCountry
        ) ?: '';
    }

    public static function isItalianShop() {
      return in_array( self::getGeneralInvoiceOptionCountryState(), [ 'IT', 'SM' ] );
    }
  
    public static function isItalianFreelanceShop() {
      $enable_tax_calculation = (bool) get_option( 'pop_pmpro_enable_tax_calculation' );
      $taxRegime              = GeneralFields::getGeneralInvoiceOptionTaxRegime();
  
      return self::isItalianShop() && ! $enable_tax_calculation && in_array( $taxRegime, [ 'RF19', 'RF02' ], true );
    }
  
    public static function isItalianStampDutyActive() {
      $enable_tax_calculation = (bool) get_option( 'pop_pmpro_enable_tax_calculation' );
      $addDuty                = OptionPage::init()->getOptions( 'add_stamp_duty' );
  
      return self::isItalianShop() && ! $enable_tax_calculation && 'on' === $addDuty;
    }

    public static function isItalianRivalsaActive() {
      $addRivalsa = OptionPage::init()->getOptions( 'add_rivalsa_inps' );

      return self::isItalianFreelanceShop() && 'on' === $addRivalsa;
    }
  
    /**
     * General store invoice country province
     *
     * @return mixed Value set for the option.
     * @since 1.0.0
     *
     */
    public static function getGeneralInvoiceOptionCountryProvince()
    {
        $country = get_option('woocommerce_default_country');
        $country = explode(':', $country);

        if (is_array($country) && count($country) === 1 && ! in_array($country[0], self::getEuCountries())) {
            return '';
        }

        return apply_filters('wc_el_inv-woocommerce_store_country_province',
            $country[1]
        ) ?: '';
    }

    /**
     * General store invoice city
     *
     * @return mixed Value set for the option.
     * @since 1.0.0
     *
     */
    public static function getGeneralInvoiceOptionCity()
    {
        $city = get_option('woocommerce_store_city');

        return apply_filters('wc_el_inv-woocommerce_store_city',
            \WcElectronInvoice\Functions\plainTextFromHtml($city, true)
        ) ?: '';
    }

    /**
     * General store invoice city
     *
     * @return mixed Value set for the option.
     * @since 1.0.0
     *
     */
    public static function getGeneralShippingLocation()
    {
        $location = get_option('woocommerce_ship_to_countries');

        return apply_filters('wc_el_inv-woocommerce_ship_to_countries',
            $location
        ) ?: '';
    }

    /**
     * General store invoice post code
     *
     * @return mixed Value set for the option.
     * @since 1.0.0
     *
     */
    public static function getGeneralInvoiceOptionPostCode()
    {
        $postcode = get_option('woocommerce_store_postcode');

        return apply_filters('wc_el_inv-woocommerce_store_postcode',
            $postcode
        ) ?: '';
    }

    /**
     * General store invoice post code
     *
     * @return mixed Value set for the option.
     * @since 1.0.0
     *
     */
    public static function getGeneralInvoiceOptionAddress()
    {
        $address = get_option('woocommerce_store_address');

        return apply_filters('wc_el_inv-woocommerce_store_address',
            \WcElectronInvoice\Functions\plainTextFromHtml($address, true)
        ) ?: '';
    }

    /**
     * General store transmitter vat number
     *
     * @return mixed Value set for the option.
     * @since 1.0.0
     *
     */
    public static function getGeneralInvoiceTransmitterOptionVatNumber()
    {
        $default = null;

        if (class_exists('WooPoPToFattureAruba\Plugin')) {
            $default = "01879020517";
        }

        $numberTransmitter = get_option(self::$optionPrefix . 'general_store_vat_number_transmitter') ?: null;

        return apply_filters('wc_el_inv-general_store_vat_number_transmitter',
            $numberTransmitter ?? $default
        ) ?: '';
    }

    /**
     * General store transmitter phone
     *
     * @return mixed Value set for the option.
     * @since 1.0.0
     *
     */
    public static function getGeneralInvoiceTransmitterOptionPhone()
    {
        $default = null;

        if (class_exists('WooPoPToFattureAruba\Plugin')) {
            $default = "05750505";
        }

        $phoneTransmitter = get_option(self::$optionPrefix . 'general_store_phone_transmitter') ?: null;

        return apply_filters('wc_el_inv-general_store_phone_transmitter',
            $phoneTransmitter ?? $default
        ) ?: '';
    }

    /**
     * General store transmitter email
     *
     * @return mixed Value set for the option.
     * @since 1.0.0
     *
     */
    public static function getGeneralInvoiceTransmitterOptionEmail()
    {
        $default = null;

        if (class_exists('WooPoPToFattureAruba\Plugin')) {
            $default = "info@arubapec.it";
        }

        $emailTransmitter = get_option(self::$optionPrefix . 'general_store_email_transmitter') ?: null;

        return apply_filters('wc_el_inv-general_store_email_transmitter',
            $emailTransmitter ?? $default
        ) ?: '';
    }

    /**
     * Sanitize
     *
     * @param $value
     * @param $option
     * @param $rawValue
     *
     * @return bool|mixed|string
     * @since 1.0.0
     *
     */
    public function sanitize($value, $option, $rawValue)
    {
        if (! in_array(self::getGeneralInvoiceOptionCountryState(), self::getEuCountries())) {
            return '';
        }

        return \WcElectronInvoice\Functions\sanitize($value);
    }

    /**
     * Notice
     *
     * @since 1.0.0
     */
    public function notice()
    {
        if (empty($_POST) || ! in_array(self::getGeneralInvoiceOptionCountryState(), self::getEuCountries())) {
            return;
        }

        // @codingStandardsIgnoreLine
        $vatCode = \WcElectronInvoice\Functions\filterInput($_POST,
            self::$optionPrefix . 'general_store_vat_number',
            FILTER_UNSAFE_RAW
        );

        // @codingStandardsIgnoreLine
        $page = \WcElectronInvoice\Functions\filterInput($_GET, 'page', FILTER_UNSAFE_RAW);
        $tab  = \WcElectronInvoice\Functions\filterInput($_GET, 'tab', FILTER_UNSAFE_RAW);

        if ('wc-settings' !== $page ||
            ($tab && ('wc-settings' === $page && 'general' !== $tab))
        ) {
            return;
        }

        // @codingStandardsIgnoreLine
        $name = \WcElectronInvoice\Functions\filterInput($_POST,
            self::$optionPrefix . 'general_store_your_name',
            FILTER_UNSAFE_RAW
        );

        // @codingStandardsIgnoreLine
        $surname = \WcElectronInvoice\Functions\filterInput($_POST,
            self::$optionPrefix . 'general_store_your_surname',
            FILTER_UNSAFE_RAW
        );

        // @codingStandardsIgnoreLine
        $company = \WcElectronInvoice\Functions\filterInput($_POST,
            self::$optionPrefix . 'general_store_company_name',
            FILTER_UNSAFE_RAW
        );

        // @codingStandardsIgnoreLine
        $phone = \WcElectronInvoice\Functions\filterInput($_POST,
            self::$optionPrefix . 'general_store_phone',
            FILTER_UNSAFE_RAW
        );

        // @codingStandardsIgnoreLine
        $email = \WcElectronInvoice\Functions\filterInput($_POST,
            self::$optionPrefix . 'general_store_email',
            FILTER_VALIDATE_EMAIL
        );

        $country = \WcElectronInvoice\Functions\filterInput($_POST,
            'woocommerce_default_country',
            FILTER_UNSAFE_RAW
        );

        $country = explode(':', $country);

        if (empty($vatCode) || strlen($vatCode) < 8) { ?>
            <div class="notice error is-dismissible">
                <p><?php esc_html_e('Please enter your valid VAT number', WC_EL_INV_TEXTDOMAIN); ?></p>
            </div>
        <?php } elseif (! empty($country) && ! empty($country[0]) && ! preg_match($this->regexVAT,
                $country[0] . $vatCode)) { ?>
            <div class="notice error is-dismissible">
                <p><?php
                    // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                    echo sprintf(__('VAT number %1$s is not correct', WC_EL_INV_TEXTDOMAIN),
                        esc_attr("<strong>{$country[0]}-{$vatCode}</strong>")
                    ); ?>
                </p>
            </div>
        <?php }

        if (! $company) { ?>

            <?php if (! $name && ! $surname) { ?>
                <div class="notice error is-dismissible">
                    <p><?php esc_html_e('Please enter your company name', WC_EL_INV_TEXTDOMAIN); ?></p>
                </div>
            <?php } ?>

            <?php if (! $name) { ?>
                <div class="notice error is-dismissible">
                    <p><?php esc_html_e('Please enter your name', WC_EL_INV_TEXTDOMAIN); ?></p>
                </div>
            <?php }

            if (! $surname) { ?>
                <div class="notice error is-dismissible">
                    <p><?php esc_html_e('Please enter your surname', WC_EL_INV_TEXTDOMAIN); ?></p>
                </div>
            <?php }
        }

        if (! $email) { ?>
            <div class="notice error is-dismissible">
                <p><?php esc_html_e('Please enter your email address', WC_EL_INV_TEXTDOMAIN); ?></p>
            </div>
        <?php }

        if (! $phone) { ?>
            <div class="notice error is-dismissible">
                <p><?php esc_html_e('Please enter your phone number', WC_EL_INV_TEXTDOMAIN); ?></p>
            </div>
        <?php }
    }
}
