<?php
namespace WTC_CHECKOUT\Core\General;
defined('ABSPATH') || exit;
class RegisterViewModel {
    public function __construct() {
        if ($this->is_name_fields_enabled()) {
            add_action('woocommerce_register_form_start', array($this, 'add_name_fields'));
            add_filter('woocommerce_registration_errors', array($this, 'validate_name_fields'), 10, 3);
            add_action('woocommerce_created_customer', array($this, 'save_name_fields'));
        }
        if ($this->is_phone_field_enabled()) {
            add_action('woocommerce_register_form_start', array($this, 'add_phone_field'));
            add_filter('woocommerce_registration_errors', array($this, 'validate_phone_field'), 10, 3);
            add_action('woocommerce_created_customer', array($this, 'save_phone_field'));
        }
    }
    private function is_name_fields_enabled() {
        return get_option('wtc_register_name_fields_enabled', 'no') === 'yes';
    }
    private function is_phone_field_enabled() {
        return get_option('wtc_register_phone_field_enabled', 'no') === 'yes';
    }
    public function add_name_fields() {
        wp_nonce_field('wtc_register_name_fields_action', 'wtc_register_name_fields_nonce');
        $first_name_value = '';
        $last_name_value = '';
        if (
            isset($_POST['wtc_register_name_fields_nonce']) &&
            wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['wtc_register_name_fields_nonce'])), 'wtc_register_name_fields_action')
        ) {
            if (!empty($_POST['first_name'])) {
                $first_name_value = esc_attr(sanitize_text_field(wp_unslash($_POST['first_name'])));
            }
            if (!empty($_POST['last_name'])) {
                $last_name_value = esc_attr(sanitize_text_field(wp_unslash($_POST['last_name'])));
            }
        }
        ?>
        <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-first">
            <label for="reg_first_name"><?php esc_html_e('Ad', 'wtc-checkout'); ?>&nbsp;<span class="required">*</span></label>
            <input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="first_name" id="reg_first_name" value="<?php echo esc_attr($first_name_value); ?>" required />
        </p>
        <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-last">
            <label for="reg_last_name"><?php esc_html_e('Soyad', 'wtc-checkout'); ?>&nbsp;<span class="required">*</span></label>
            <input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="last_name" id="reg_last_name" value="<?php echo esc_attr($last_name_value); ?>" required />
        </p>
        <div class="clear"></div>
        <?php
    }
    public function add_phone_field() {
        wp_nonce_field('wtc_register_phone_field_action', 'wtc_register_phone_field_nonce');
        $phone_value = '';
        if (
            isset($_POST['wtc_register_phone_field_nonce']) &&
            wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['wtc_register_phone_field_nonce'])), 'wtc_register_phone_field_action')
        ) {
            if (!empty($_POST['billing_phone'])) {
                $phone_value = esc_attr(sanitize_text_field(wp_unslash($_POST['billing_phone'])));
            }
        }
        ?>
        <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
            <label for="reg_billing_phone"><?php esc_html_e('Telefon', 'wtc-checkout'); ?>&nbsp;<span class="required">*</span></label>
            <input type="tel" class="woocommerce-Input woocommerce-Input--text input-text" name="billing_phone" id="reg_billing_phone" value="<?php echo esc_attr($phone_value); ?>" required />
        </p>
        <?php
    }
    public function validate_name_fields($errors, $username, $email) {
        if (is_checkout()) {
            return $errors;
        }
        if (!isset($_POST['wtc_register_name_fields_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['wtc_register_name_fields_nonce'])), 'wtc_register_name_fields_action')) {
            $errors->add('nonce_error', __('Form güvenlik doğrulaması başarısız oldu. Lütfen tekrar deneyin.', 'wtc-checkout'));
            return $errors;
        }
        if (empty($_POST['first_name'])) {
            $errors->add('first_name_error', __('Ad alanı zorunludur.', 'wtc-checkout'));
        }
        if (empty($_POST['last_name'])) {
            $errors->add('last_name_error', __('Soyad alanı zorunludur.', 'wtc-checkout'));
        }
        return $errors;
    }
    public function validate_phone_field($errors, $username, $email) {
        if (is_checkout()) {
            return $errors;
        }
        if (!isset($_POST['wtc_register_phone_field_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['wtc_register_phone_field_nonce'])), 'wtc_register_phone_field_action')) {
            $errors->add('nonce_error', __('Form güvenlik doğrulaması başarısız oldu. Lütfen tekrar deneyin.', 'wtc-checkout'));
            return $errors;
        }
        if (empty($_POST['billing_phone'])) {
            $errors->add('billing_phone_error', __('Telefon numarası zorunludur.', 'wtc-checkout'));
        } else {
            $phone = sanitize_text_field(wp_unslash($_POST['billing_phone']));
            if (!preg_match('/^\+90 \(\d{3}\) \d{3} \d{2} \d{2}$/', $phone)) {
                $errors->add('billing_phone_error', __('Geçerli bir telefon numarası giriniz.', 'wtc-checkout'));
            }
        }
        return $errors;
    }
    public function save_name_fields($customer_id) {
        if (!isset($_POST['wtc_register_name_fields_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['wtc_register_name_fields_nonce'])), 'wtc_register_name_fields_action')) {
            return;
        }
        if (isset($_POST['first_name'])) {
            $first_name = sanitize_text_field(wp_unslash($_POST['first_name']));
            update_user_meta($customer_id, 'first_name', $first_name);
            update_user_meta($customer_id, 'billing_first_name', $first_name);
            update_user_meta($customer_id, 'shipping_first_name', $first_name);
        }
        if (isset($_POST['last_name'])) {
            $last_name = sanitize_text_field(wp_unslash($_POST['last_name']));
            update_user_meta($customer_id, 'last_name', $last_name);
            update_user_meta($customer_id, 'billing_last_name', $last_name);
            update_user_meta($customer_id, 'shipping_last_name', $last_name);
        }
    }
    public function save_phone_field($customer_id) {
        if (!isset($_POST['wtc_register_phone_field_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['wtc_register_phone_field_nonce'])), 'wtc_register_phone_field_action')) {
            return;
        }
        if (isset($_POST['billing_phone'])) {
            $phone = sanitize_text_field(wp_unslash($_POST['billing_phone']));
            update_user_meta($customer_id, 'billing_phone', $phone);
        }
    }
} 