<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
    add_action('wp_ajax_tfree_get_recent_order', 'tfree_get_recent_order');

    function tfree_get_recent_order()
    {
        if (!current_user_can('manage_woocommerce') && !current_user_can('manage_options')) {
            wp_send_json_error(array('status' => 'forbidden'), 403);
            wp_die();
        }

        $nonce = isset($_SERVER['HTTP_X_WP_NONCE']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_X_WP_NONCE'])) : '';
        if (!$nonce || !wp_verify_nonce($nonce, 'tfree_nonce')) {
            wp_send_json_error(array('status' => 'error'));
            wp_die();
        }

        $orders = wc_get_orders(array(
            'limit' => 1,
            'orderby' => 'date',
            'order' => 'DESC'
        ));
        if (!empty($orders)) {
            $order_id = $orders[0]->get_id();
            $order_url = admin_url('post.php?post=' . $order_id . '&action=edit');
        } else {
            $order_url = admin_url('edit.php?post_type=shop_order');
        }

        $response = array(
            'status' => 'success',
            'order_url' => $order_url
        );
        wp_send_json_success($response);
        wp_die();
    }

    add_action('wp_ajax_tfree_get_carriers', 'tfree_get_carriers');

    function tfree_get_carriers()
    {
        if (!current_user_can('manage_woocommerce') && !current_user_can('manage_options')) {
            wp_send_json_error(array('status' => 'forbidden'), 403);
            wp_die();
        }

        $nonce = isset($_SERVER['HTTP_X_WP_NONCE']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_X_WP_NONCE'])) : '';
        if (!$nonce || !wp_verify_nonce($nonce, 'tfree_nonce')) {
            wp_send_json_error(array('status' => 'error'));
            wp_die();
        }

        $search_term = isset($_REQUEST['search_term']) ? sanitize_text_field(wp_unslash($_REQUEST['search_term'])) : '';

        $all_options = tfree_get_courier_list();
        $filtered_options = array_filter($all_options, function($option) use ($search_term) {
            return stripos($option['name'], $search_term) !== false;
        });
        $options = array_slice($filtered_options, 0, 10);
        $response = array(
            'status' => 'success',
            'carriers' => $options
        );
        wp_send_json_success($response);
        wp_die();
    }

    add_action( 'wp_ajax_tfree_dismiss_quick_start_info', 'tfree_dismiss_quick_start_info' );

    function tfree_dismiss_quick_start_info()
    {
        if (!current_user_can('manage_woocommerce') && !current_user_can('manage_options')) {
            wp_send_json_error(array('status' => 'forbidden'), 403);
            wp_die();
        }

        $nonce = isset($_SERVER['HTTP_X_WP_NONCE']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_X_WP_NONCE'])) : '';
        if (!$nonce || !wp_verify_nonce($nonce, 'tfree_nonce')) {
            wp_send_json_error(array('status' => 'error'));
            wp_die();
        }
        add_option('trackfree_hide_quick_start_info');
        update_option('trackfree_hide_quick_start_info', 1);
        wp_die();
    }
}
?>
