<?php
namespace orderflow\Core;

class Base {
    /**
     * Checks if current request is a system context (CRON, AJAX, REST)
     *
     * @return bool
     */
    public static function is_system_request_context() {
        return defined('DOING_CRON') || wp_doing_ajax() || defined('DOING_AJAX') || defined('DOING_REST');
    }

    /**
     * Checks if current request is in preview mode
     *
     * @return bool
     */
    public static function is_preview_mode() {
        return is_preview() || isset($_GET['elementor-preview']) || isset($_GET['wp_scrape_key']);
    }

    /**
     * Generates hashed customer profile data
     *
     * @param int|null $user_id User ID (null for current user)
     * @param array $user_data Existing user data to append
     * @return array
     */
    public static function generate_hashed_customer_profile($user_id = null, $user_data = []) {
        $current_user_id = $user_id ?? get_current_user_id();
        $customer = new \WC_Customer($current_user_id);

        $fields = [
            'fn' => $customer->get_billing_first_name(),
            'ln' => $customer->get_billing_last_name(),
            'ph' => $customer->get_billing_phone(),
            'em' => $customer->get_billing_email(),
            'ct' => $customer->get_billing_city(),
            'zp' => $customer->get_billing_postcode(),
            'st' => $customer->get_billing_state(),
            'country' => $customer->get_billing_country(),
        ];

        foreach ($fields as $key => $value) {
            if (!array_key_exists($key, $user_data) && !empty($value)) {
                $user_data[$key] = hash('sha256', $value);
            }
        }

        return $user_data;
    }

    /**
     * Generates hashed order billing data
     *
     * @param int|null $order_id Order ID
     * @param array $user_data Existing user data to append
     * @return array
     */
    public static function generate_hashed_order_billing_data($order_id = null, $user_data = []) {
        if (!$order_id || !($order = wc_get_order($order_id))) {
            return $user_data;
        }

        $fields = [
            'fn'      => 'get_billing_first_name',
            'ln'      => 'get_billing_last_name',
            'ph'      => 'get_billing_phone',
            'em'      => 'get_billing_email',
            'ct'      => 'get_billing_city',
            'zp'      => 'get_billing_postcode',
            'st'      => 'get_billing_state',
            'country' => 'get_billing_country'
        ];

        foreach ($fields as $key => $method) {
            $value = $order->$method();
            if (!empty($value)) {
                $user_data[$key] = hash('sha256', $value);
            }
        }

        return $user_data;
    }

    /**
     * Collects client request data (IP and user agent)
     *
     * @param array $user_data Existing user data to append
     * @return array
     */
    public static function collect_client_request_data($user_data = []) {
        $user_data['client_ip_address'] = $_SERVER['REMOTE_ADDR'] ?? '';
        $user_data['client_user_agent'] = $_SERVER['HTTP_USER_AGENT'] ?? '';
        return $user_data;
    }

    /**
     * Terminates execution if Facebook payload option is enabled
     */
    public static function terminate_after_facebook_payload() {
        if (get_option('ads_terminate_after_facebook_payload', false)) {
            exit;
        }
    }

    /**
     * Fetches customer record from database
     *
     * @param string $data_type Data type (phone_number, email_address, etc.)
     * @param string $data_value Data value
     * @return object|false
     */
    public static function fetch_customer_record($data_type = null, $data_value = null) {
        if (!$data_type || !$data_value) {
            return false;
        }

        global $wpdb;
        $table_name = $wpdb->prefix . 'ads_customers_data';
        $sql = $wpdb->prepare(
            "SELECT * FROM {$table_name} WHERE data_type = %s AND data_value = %s",
            $data_type,
            $data_value
        );

        return $wpdb->get_row($sql);
    }

    /**
     * Determines customer access class based on record
     *
     * @param object $row Database record
     * @return string CSS class name
     */
    public static function determine_customer_access_class($row) {
        return ($row && is_object($row) && $row->data_access === 'blocked')
            ? 'ads-customer-access-blocked'
            : 'ads-customer-access-allowed';
    }

    /**
     * Normalizes phone number by removing prefixes and non-numeric characters
     *
     * @param string $phone Phone number
     * @return string Normalized phone number
     */
    public static function normalize_phone_number($phone) {
        $phone = preg_replace('/^\+?88/', '', $phone); // Remove +88 or 88
        $phone = preg_replace('/^\+/', '', $phone);    // Remove + if still at start
        $phone = preg_replace('/[^0-9]/', '', $phone); // Remove all except numbers
        return $phone;
    }
}