<?php
if ( ! defined( 'ABSPATH' ) ) exit;
class ShippingGuard_Customer {
    public function init() {
        // Register the endpoint early so WC knows about it
        add_action( 'init', array( $this, 'register_endpoint' ) );
        add_action( 'woocommerce_view_order', array( $this, 'display_claim_section' ), 20 );
        add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
        add_action( 'woocommerce_account_shippingguard-claims_endpoint', array( $this, 'claims_endpoint_content' ) );
        add_filter( 'woocommerce_account_menu_items', array( $this, 'add_account_menu_item' ) );
        add_filter( 'woocommerce_get_query_vars', array( $this, 'add_query_vars' ) );
    }

    public function register_endpoint() {
        add_rewrite_endpoint( 'shippingguard-claims', EP_ROOT | EP_PAGES );
    }

    public static function flush_rewrite_rules() {
        add_rewrite_endpoint( 'shippingguard-claims', EP_ROOT | EP_PAGES );
        flush_rewrite_rules();
    }

    public function add_query_vars( $vars ) {
        $vars['shippingguard-claims'] = 'shippingguard-claims';
        return $vars;
    }

    public function add_account_menu_item( $items ) {
        $logout = isset( $items['customer-logout'] ) ? $items['customer-logout'] : null;
        unset( $items['customer-logout'] );
        $items['shippingguard-claims'] = 'My Claims';
        if ( $logout ) {
            $items['customer-logout'] = $logout;
        }
        return $items;
    }

    public function claims_endpoint_content() {
        echo '<div id="shippingguard-my-claims-root"></div>';
    }

    public function enqueue_scripts() {
        if ( is_wc_endpoint_url( 'view-order' ) || is_wc_endpoint_url( 'shippingguard-claims' ) ) {
            wp_enqueue_script(
                'shippingguard-customer',
                SHIPPINGGUARD_URL . 'assets/customer.js',
                array( 'jquery' ),
                SHIPPINGGUARD_VERSION,
                true
            );
            wp_localize_script( 'shippingguard-customer', 'shippingGuardCustomerData', array(
                'root'     => esc_url_raw( rest_url() ),
                'nonce'    => wp_create_nonce( 'wp_rest' ),
                'order_id' => absint( get_query_var( 'view-order' ) ),
            ) );
        }
    }

    public function display_claim_section( $order_id ) {
        global $wpdb;
        $table_orders = esc_sql( $wpdb->prefix . 'shippingguard_orders' );
        $order_id_safe = absint( $order_id );

        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Custom table query for order protection status.
        $is_protected = $wpdb->get_var( $wpdb->prepare(
            // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name interpolation is safe with esc_sql.
            "SELECT is_protected FROM $table_orders WHERE order_id = %d",
            $order_id_safe
        ) );

        if ( ! $is_protected ) return;

        echo '<div id="shippingguard-claim-form-root" data-order-id="' . esc_attr( $order_id ) . '"></div>';
    }
}
