<?php

if (!defined('ABSPATH')) { exit; }

if ( class_exists( 'AIEO_Runtime_Fulfillment_Tracking' ) ) {
    return;
}

class AIEO_Runtime_Fulfillment_Tracking {

    
    public static function register() {
        add_action('woocommerce_view_order', array(__CLASS__, 'on_view_order'), 25, 1);
        
        
        
        add_filter('woocommerce_account_orders_columns', array(__CLASS__, 'add_orders_list_tracking_column'), 20);
        add_action('woocommerce_my_account_my_orders_column_aieo-tracking', array(__CLASS__, 'render_orders_list_tracking_cell'), 10, 1);
    }

    
    public static function on_view_order($order_id) {
        echo self::render_block((int) $order_id, 'my_account'); 
    }

    
    public static function add_orders_list_tracking_column($columns) {
        if (!is_array($columns)) { return $columns; }
        $out = array();
        foreach ($columns as $key => $name) {
            if ('order-actions' === $key) {
                $out['aieo-tracking'] = esc_html__('Tracking', 'ai-eshop-optimizer');
            }
            $out[$key] = $name;
        }
        if (!isset($out['aieo-tracking'])) {
            $out['aieo-tracking'] = esc_html__('Tracking', 'ai-eshop-optimizer');
        }
        return $out;
    }

    
    public static function render_orders_list_tracking_cell($order) {
        if (!is_a($order, 'WC_Order')) { return; }
        if (!class_exists('AIEO_Runtime_Fulfillment_Vouchers')) { echo '&ndash;'; return; }

        $vouchers = AIEO_Runtime_Fulfillment_Vouchers::get_for_order($order->get_id());
        if (empty($vouchers)) { echo '&ndash;'; return; }

        $out = array();
        foreach ($vouchers as $v) {
            $no = isset($v['voucher_no']) ? (string) $v['voucher_no'] : '';
            if ($no === '') { continue; }
            $carrier = isset($v['carrier']) ? (string) $v['carrier'] : '';
            $url = class_exists('AIEO_Runtime_Fulfillment_URLs')
                ? AIEO_Runtime_Fulfillment_URLs::tracking_url($carrier, $no)
                : '';
            $out[] = $url !== ''
                ? '<a href="' . esc_url($url) . '" target="_blank" rel="noopener"><code>' . esc_html($no) . '</code></a>'
                : '<code>' . esc_html($no) . '</code>';
        }
        echo $out ? wp_kses_post(implode('<br>', $out)) : '&ndash;';
    }

    
    public static function render_block($order_id, $context = 'my_account') {
        $order_id = (int) $order_id;
        if ($order_id <= 0) { return ''; }
        if (!class_exists('AIEO_Runtime_Fulfillment_Vouchers')) { return ''; }

        $vouchers = AIEO_Runtime_Fulfillment_Vouchers::get_for_order($order_id);
        if (empty($vouchers)) { return ''; }

        
        
        
        ob_start();
        ?>
        <section class="aieo-fulfillment-tracking aieo-fulfillment-tracking--<?php echo esc_attr($context); ?>">
            <h3><?php esc_html_e('Παρακολούθηση αποστολής', 'ai-eshop-optimizer'); ?></h3>
            <table class="aieo-fulfillment-tracking__table">
                <thead>
                <tr>
                    <th><?php esc_html_e('Μεταφορέας', 'ai-eshop-optimizer'); ?></th>
                    <th><?php esc_html_e('Αριθμός αποστολής', 'ai-eshop-optimizer'); ?></th>
                    <th><?php esc_html_e('Κατάσταση', 'ai-eshop-optimizer'); ?></th>
                </tr>
                </thead>
                <tbody>
                <?php foreach ($vouchers as $v):
                    $carrier_label = AIEO_Runtime_Fulfillment_URLs::carrier_label((string) $v['carrier']);
                    $tracking_url  = AIEO_Runtime_Fulfillment_URLs::tracking_url((string) $v['carrier'], (string) $v['voucher_no']);
                ?>
                <tr>
                    <td><?php echo esc_html($carrier_label !== '' ? $carrier_label : $v['carrier']); ?></td>
                    <td>
                        <?php if ($tracking_url !== ''): ?>
                            <a href="<?php echo esc_url($tracking_url); ?>" target="_blank" rel="noopener"><code><?php echo esc_html((string) $v['voucher_no']); ?></code></a>
                        <?php else: ?>
                            <code><?php echo esc_html((string) $v['voucher_no']); ?></code>
                        <?php endif; ?>
                    </td>
                    <td><?php echo esc_html((string) $v['status']); ?></td>
                </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </section>
        <?php
        return (string) ob_get_clean();
    }
}
