<?php
namespace WTC_CHECKOUT\Modules\Forms\Services;
defined('ABSPATH') || exit;
class UserProfileService {
    private static $instance = null;
    public static function get_instance() {
        if (self::$instance === null) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    private function __construct() {
        $this->init_hooks();
    }
    private function init_hooks() {
        add_action('show_user_profile', array($this, 'add_customer_meta_fields'));
        add_action('edit_user_profile', array($this, 'add_customer_meta_fields'));
        add_action('personal_options_update', array($this, 'save_customer_meta_fields'));
        add_action('edit_user_profile_update', array($this, 'save_customer_meta_fields'));
    }
    public function add_customer_meta_fields($user) {
        if (!current_user_can('manage_options')) {
            return;
        }
        ?>
        <?php wp_nonce_field('wtc_save_user_profile', 'wtc_user_profile_nonce'); ?>
        <h3><?php esc_html_e('Fatura Bilgileri','wtc-checkout'); ?></h3>
        <table class="form-table">
            <tr>
                <th><label for="billing_invoice_type"><?php esc_html_e('Fatura Tipi', 'wtc-checkout'); ?></label></th>
                <td>
                    <?php 
                    $invoice_type = get_user_meta($user->ID, 'billing_invoice_type', true);
                    echo $invoice_type === 'corporate' ? 'Kurumsal' : 'Bireysel';
                    ?>
                </td>
            </tr>
            <?php if ($invoice_type === 'individual') :?>
            <tr>
                <th><label for="billing_tckn"><?php esc_html_e('TC Kimlik No', 'wtc-checkout'); ?></label></th>
                <td>
                    <input type="text" name="billing_tckn" id="billing_tckn" 
                        value="<?php echo esc_attr(get_user_meta($user->ID, 'billing_tckn', true)); ?>" 
                        class="regular-text" />
                </td>
            </tr>
            <?php endif; ?>
            <?php if ($invoice_type === 'corporate') :?>
            <tr>
                <th><label for="billing_tax_number"><?php esc_html_e('Vergi No', 'wtc-checkout'); ?></label></th>
                <td>
                    <input type="text" name="billing_tax_number" id="billing_tax_number" 
                        value="<?php echo esc_attr(get_user_meta($user->ID, 'billing_tax_number', true)); ?>" 
                        class="regular-text" />
                </td>
            </tr>
            <tr>
                <th><label for="billing_tax_office"><?php esc_html_e('Vergi Dairesi', 'wtc-checkout'); ?></label></th>
                <td>
                    <input type="text" name="billing_tax_office" id="billing_tax_office" 
                        value="<?php echo esc_attr(get_user_meta($user->ID, 'billing_tax_office', true)); ?>" 
                        class="regular-text" />
                </td>
            </tr>
            <?php endif; ?>
        </table>
        <h3><?php esc_html_e('Teslimat Bilgileri','wtc-checkout'); ?></h3>
        <table class="form-table">
            <tr>
                <th><label for="shipping_name"><?php esc_html_e('Teslimat Adresi Adı', 'wtc-checkout'); ?></label></th>
                <td>
                    <input type="text" name="shipping_name" id="shipping_name" 
                        value="<?php echo esc_attr(get_user_meta($user->ID, 'shipping_name', true)); ?>" 
                        class="regular-text" />
                </td>
            </tr>
        </table>
        <?php
    }
    public function save_customer_meta_fields($user_id) {
        if (!current_user_can('manage_options')) {
            return false;
        }
        if (!isset($_POST['wtc_user_profile_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['wtc_user_profile_nonce'])), 'wtc_save_user_profile')) {
            return false;
        }
        if (isset($_POST['billing_tckn'])) {
            update_user_meta($user_id, 'billing_tckn', sanitize_text_field(wp_unslash($_POST['billing_tckn'])));
        }
        if (isset($_POST['billing_tax_number'])) {
            update_user_meta($user_id, 'billing_tax_number', sanitize_text_field(wp_unslash($_POST['billing_tax_number'])));
        }
        if (isset($_POST['billing_tax_office'])) {
            update_user_meta($user_id, 'billing_tax_office', sanitize_text_field(wp_unslash($_POST['billing_tax_office'])));
        }
        if (isset($_POST['shipping_name'])) {
            update_user_meta($user_id, 'shipping_name', sanitize_text_field(wp_unslash($_POST['shipping_name'])));
        }
        return true;
    }
}