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

/*------------------------------------------------------------------------------
  Translation Meta Box Registration
------------------------------------------------------------------------------*/
add_action( 'add_meta_boxes', function () {
    add_meta_box(
        'reeid-translation-meta-box',
        __( 'REEID TRANSLATION', 'reeid-translate' ),
        'reeid_render_meta_box',
        array( 'post', 'page', 'product' ),
        'side',
        'high'
    );
} );

/*------------------------------------------------------------------------------
  Meta Box Wrapper
------------------------------------------------------------------------------*/
if ( ! function_exists( 'reeid_render_meta_box' ) ) {
    function reeid_render_meta_box( $post ) {
        if ( ! $post instanceof WP_Post ) {
            $post = get_post( $post );
        }

        wp_nonce_field( 'reeid_translate_nonce_action', 'reeid_translate_nonce' );
        reeid_render_meta_box_ui_controls( $post );
    }
}

/*------------------------------------------------------------------------------
  Meta Box UI Controls
------------------------------------------------------------------------------*/
if ( ! function_exists( 'reeid_render_meta_box_ui_controls' ) ) {
    function reeid_render_meta_box_ui_controls( $post ) {

        if ( ! $post instanceof WP_Post ) {
            $post = get_post( $post );
        }

        if ( ! $post instanceof WP_Post ) {
            return;
        }

        $languages = function_exists( 'reeid_get_allowed_languages' )
            ? (array) reeid_get_allowed_languages()
            : array();

        $source_lang = (string) get_post_meta( $post->ID, '_reeid_translation_lang', true );

        if ( '' === $source_lang ) {
            $source_lang = (string) get_option( 'reeid_translation_source_lang', 'en' );
        }

        $saved_lang = (string) get_post_meta( $post->ID, '_reeid_target_lang', true );

        if ( ! $saved_lang || $saved_lang === $source_lang ) {
            foreach ( $languages as $code => $_label ) {
                if ( $code !== $source_lang ) {
                    $saved_lang = $code;
                    break;
                }
            }
        }


        $tones = get_option( 'reeid_tone_presets', array() );
        if ( ! is_array( $tones ) || empty( $tones ) ) {
            $tones = array(
                'neutral' => 'Neutral',
            );
        }

        $current_tone = '';
        if ( function_exists( 'reeid_get_effective_tone' ) ) {
            $current_tone = (string) reeid_get_effective_tone( (int) $post->ID );
        }
        $current_tone = strtolower( sanitize_text_field( $current_tone ) );
        ?>
        <div class="reeid-field">
            <strong><?php esc_html_e( 'Select target language:', 'reeid-translate' ); ?></strong>
            <select name="reeid_target_lang" id="reeid_target_lang">
                <?php
                foreach ( $languages as $code => $label ) {
                    if ( $code === $source_lang ) {
                        continue;
                    }

                    printf(
                        '<option value="%s"%s>%s</option>',
                        esc_attr( $code ),
                        selected( $saved_lang, $code, false ),
                        esc_html( $label )
                    );
                }
                ?>
            </select>
        </div>

        <div class="reeid-field">
            <strong><?php esc_html_e( 'Tone override:', 'reeid-translate' ); ?></strong>
            <select name="reeid_tone_override" id="reeid_tone_override" style="width:100%;margin-top:6px;">
                <option value=""><?php esc_html_e( 'Use default', 'reeid-translate' ); ?></option>
                <?php foreach ( $tones as $tone_key => $tone_label ) : ?>
                    <option value="<?php echo esc_attr( (string) $tone_key ); ?>" <?php selected( $current_tone, (string) $tone_key ); ?>>
                        <?php echo esc_html( (string) $tone_label ); ?>
                    </option>
                <?php endforeach; ?>
            </select>
        </div>



        <div class="reeid-field">
            <strong><?php esc_html_e( 'Translation Status', 'reeid-translate' ); ?></strong><br>
            <label>
                <input type="radio" name="reeid_publish_mode" value="publish" checked>
                <?php esc_html_e( 'Translate and Publish', 'reeid-translate' ); ?>
            </label><br>
            <label>
                <input type="radio" name="reeid_publish_mode" value="draft">
                <?php esc_html_e( 'Translate and Save as Draft', 'reeid-translate' ); ?>
            </label>
        </div>

        <div class="reeid-buttons">
            <button id="reeid-translate-btn" class="reeid-button primary" data-postid="<?php echo esc_attr( $post->ID ); ?>">
                <?php esc_html_e( 'Translate', 'reeid-translate' ); ?>
            </button>
        </div>

        <div id="reeid-status"></div>
        <?php
    }
}