<?php
defined('ABSPATH') || exit;

if (! function_exists('reeid_translate_product')) {
    function reeid_translate_product(
        int $src_id,
        string $src_lang,
        string $dst_lang,
        string $tone,
        string $prompt,
        int $dst_id,
        string $publish_mode = 'publish'
    ) {
        if (! class_exists('WC_Product')) {
            return new WP_Error('no_wc', 'WooCommerce not active.');
        }

        $src_product = wc_get_product($src_id);
        if (! $src_product) {
            return new WP_Error('no_product', 'Source product missing.');
        }

        // === Translate title & slug ===
        $title_src = $src_product->get_name();
        $title_tr  = reeid_translate_html_with_openai($title_src, $src_lang, $dst_lang, 'classic', $tone);
        if (is_wp_error($title_tr) || empty($title_tr)) {
            $title_tr = $title_src;
        }

        $slug_src = $src_product->get_slug();
        $slug_tr  = reeid_translate_html_with_openai($slug_src, $src_lang, $dst_lang, 'classic', $tone);
        if (is_wp_error($slug_tr) || empty($slug_tr)) {
            $slug_tr = $slug_src;
        }

        // === Translate long + short description via main engine ===
        $long_src  = (string) $src_product->get_description();
        $short_src = (string) $src_product->get_short_description();

        $long_tr = $long_src !== ''
    ? (array) reeid_translate_via_openai_with_slug($long_src, $dst_lang, [
        'post_id' => $src_id,
        'source_lang' => $src_lang,
        'target_lang' => $dst_lang,
        'tone' => $tone,
        'prompt' => $prompt,
        'mode' => 'single',
    ])
    : ['content' => ''];

$long_tr = function_exists('reeid_normalize_translation_pack')
    ? reeid_normalize_translation_pack((array) $long_tr)
    : (array) $long_tr;

        $short_tr = $short_src !== ''
            ? reeid_translate_html_with_openai($short_src, $src_lang, $dst_lang, 'classic', $tone)
            : '';

        if (is_wp_error($short_tr) || $short_tr === null) {
            $short_tr = $short_src;
        }

        $dst_post = [
            'ID'           => $dst_id,
            'post_title'   => is_array($long_tr) && isset($long_tr['title']) && $long_tr['title'] !== '' ? (string) $long_tr['title'] : (string) $title_tr,
            'post_name'    => is_array($long_tr) && isset($long_tr['slug']) && $long_tr['slug'] !== '' ? (string) $long_tr['slug'] : sanitize_title((string) $slug_tr),
            'post_content' => is_array($long_tr) && isset($long_tr['content']) ? (string) $long_tr['content'] : $long_src,
            'post_excerpt' => (string) $short_tr,
            'post_status'  => ($publish_mode === 'draft') ? 'draft' : 'publish',
            'post_type'    => 'product',
        ];

        $new_id = wp_update_post(wp_slash($dst_post), true);
        if (is_wp_error($new_id) || ! $new_id) {
            return $new_id;
        }

        $dst_product = wc_get_product($new_id);
        if (! $dst_product) {
            return new WP_Error('no_dst_product', 'Translated product missing after update.');
        }

        // === Copy core Woo data ===
        if (function_exists('reeid_copy_wc_data_to_translation')) {
            reeid_copy_wc_data_to_translation($src_id, $new_id);
        }

        // === Translate/copy attributes ===
        if (function_exists('reeid_translate_product_attributes')) {
            reeid_translate_product_attributes($src_product, $dst_product, $src_lang, $dst_lang, $tone);
        }

        // === Copy selected product props when setter/getter exists ===
        $props = [
            'catalog_visibility',
            'featured',
            'menu_order',
            'reviews_allowed',
            'purchase_note',
            'sold_individually',
            'virtual',
            'downloadable',
            'tax_status',
            'tax_class',
            'manage_stock',
            'backorders',
            'stock_status',
            'weight',
            'length',
            'width',
            'height',
        ];

        foreach ($props as $prop) {
            $getter = 'get_' . $prop;
            $setter = 'set_' . $prop;

            if (method_exists($src_product, $getter) && method_exists($dst_product, $setter)) {
                $dst_product->$setter($src_product->$getter());
            }
        }

        $dst_product->save();

        return $new_id;
    }
}