<?php
/**
 * Automatic translation settings view.
 *
 * @var array $data Data for the view.
 *
 * @package WPR_Translate
 */

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

// Normalize settings data for the view.
$settings = isset( $data['auto_translation'] ) && is_array( $data['auto_translation'] ) ? $data['auto_translation'] : array();
$post_types = isset( $data['post_types'] ) && is_array( $data['post_types'] ) ? $data['post_types'] : array();
$defaults = wp_parse_args(
    $settings,
    array(
        'enabled'        => false,
        'engine'         => 'openai',
        'openai_api_key' => '',
        'openai_system_prompt' => 'You are a translator that converts WordPress post data into the requested language.',
        'openai_user_prompt'   => 'Translate the following WordPress post payload into the %1$s language.',
    )
);

$engines = array(
    'openai' => __( 'OpenAI', 'wpr-translate' ),
);
?>
<div class="wrap wpr-translate-admin">
    <h1><?php esc_html_e( 'Automatic Translation', 'wpr-translate' ); ?></h1>

    <form method="post" action="options.php" class="wpr-card wpr-settings-card">
        <?php settings_fields( 'wpr_translate_auto_translation' ); ?>

        <div class="wpr-settings-field">
            <div class="wpr-settings-label">
                <label for="wpr-auto-translation-enabled"><?php esc_html_e( 'Automatic Translation', 'wpr-translate' ); ?></label>
            </div>
            <div class="wpr-settings-control">
                <label class="wpr-toggle">
                    <input type="checkbox" name="wpr_translate_auto_translation[enabled]" id="wpr-auto-translation-enabled" value="1" <?php checked( ! empty( $defaults['enabled'] ) ); ?> />
                    <span><?php esc_html_e( 'Enable automatic translation for new content.', 'wpr-translate' ); ?></span>
                </label>
            </div>
        </div>

        <div class="wpr-settings-field">
            <div class="wpr-settings-label">
                <label for="wpr-auto-translation-engine"><?php esc_html_e( 'Translation Engine', 'wpr-translate' ); ?></label>
            </div>
            <div class="wpr-settings-control">
                <select name="wpr_translate_auto_translation[engine]" id="wpr-auto-translation-engine">
                    <?php foreach ( $engines as $engine_key => $engine_label ) : ?>
                        <option value="<?php echo esc_attr( $engine_key ); ?>" <?php selected( $defaults['engine'], $engine_key ); ?>><?php echo esc_html( $engine_label ); ?></option>
                    <?php endforeach; ?>
                </select>
                <p class="description"><?php esc_html_e( 'Choose the service used to generate automatic translations.', 'wpr-translate' ); ?></p>
            </div>
        </div>

        <div class="wpr-settings-field">
            <div class="wpr-settings-label">
                <label for="wpr-openai-api-key"><?php esc_html_e( 'OpenAI API Key', 'wpr-translate' ); ?></label>
            </div>
            <div class="wpr-settings-control">
                <input type="password" name="wpr_translate_auto_translation[openai_api_key]" id="wpr-openai-api-key" value="<?php echo esc_attr( $defaults['openai_api_key'] ); ?>" />
                <p class="description"><?php esc_html_e( 'Stored securely in the WordPress options table.', 'wpr-translate' ); ?></p>
                <p class="description">
                    <?php
                    printf(
                        /* translators: %s: OpenAI API keys URL. */
                        __( 'Get your API key from %s.', 'wpr-translate' ),
                        '<a href="https://platform.openai.com/settings/organization/api-keys" target="_blank" rel="noopener noreferrer">platform.openai.com</a>'
                    );
                    ?>
                </p>
            </div>
        </div>

        <div class="wpr-settings-field">
            <div class="wpr-settings-label">
                <label for="wpr-openai-system-prompt"><?php esc_html_e( 'System prompt', 'wpr-translate' ); ?></label>
            </div>
            <div class="wpr-settings-control">
                <textarea name="wpr_translate_auto_translation[openai_system_prompt]" id="wpr-openai-system-prompt" rows="3" class="wpr-auto-translation-textarea"><?php echo esc_textarea( $defaults['openai_system_prompt'] ); ?></textarea>
                <p class="description"><?php esc_html_e( 'Base instruction sent as the system message before translation.', 'wpr-translate' ); ?></p>
            </div>
        </div>

        <div class="wpr-settings-field">
            <div class="wpr-settings-label">
                <label for="wpr-openai-user-prompt"><?php esc_html_e( 'User prompt', 'wpr-translate' ); ?></label>
            </div>
            <div class="wpr-settings-control">
                <textarea name="wpr_translate_auto_translation[openai_user_prompt]" id="wpr-openai-user-prompt" rows="3" class="wpr-auto-translation-textarea"><?php echo esc_textarea( $defaults['openai_user_prompt'] ); ?></textarea>
                <p class="description"><?php esc_html_e( 'Base prompt used before appending JSON guidance and the payload.', 'wpr-translate' ); ?></p>
            </div>
        </div>

        <div class="wpr-settings-actions">
            <input type="submit" class="button button-primary" value="<?php esc_attr_e( 'Save Settings', 'wpr-translate' ); ?>" />
        </div>
    </form>

    <?php // Display available post types so admins can choose which ones to translate. ?>
        <div class="wpr-card wpr-settings-card wpr-auto-translation-post-types">
            <h2><?php esc_html_e( 'Select post type for translation', 'wpr-translate' ); ?></h2>
        <div class="wpr-explanation wpr-auto-translation-explanation">
            <?php echo esc_html( 'Choose whether each post type should be auto-translated. "Translate" will queue items for automatic translation, while "Do Not Translate" will skip them. Your selection controls what the bulk auto-translation will process when you start it.' ); ?>
        </div>

        <div class="wpr-settings-field wpr-auto-translation-target">
            <div class="wpr-settings-label">
                <?php esc_html_e( 'Translate into language', 'wpr-translate' ); ?>
            </div>
            <div class="wpr-settings-control">
                <select id="wpr-auto-translation-language" class="wpr-auto-translation-language" name="wpr_translate_auto_translation[target_language]">
                    <option value=""><?php esc_html_e( 'Select language', 'wpr-translate' ); ?></option>
                    <?php
                    if ( ! empty( $data['languages'] ) && is_array( $data['languages'] ) ) {
                        foreach ( $data['languages'] as $language ) {
                            if ( ! isset( $language['value'], $language['label'] ) ) {
                                continue;
                            }

                            $value     = sanitize_text_field( $language['value'] );
                            $label     = sanitize_text_field( $language['label'] );
                            $selected  = ( ! empty( $data['target_language'] ) && $data['target_language'] === $value ) ? 'selected' : '';
                            ?>
                            <option value="<?php echo esc_attr( $value ); ?>" <?php echo esc_attr( $selected ); ?>><?php echo esc_html( $label ); ?></option>
                            <?php
                        }
                    }
                    ?>
                </select>
            </div>
        </div>

        <?php if ( ! empty( $post_types ) ) : ?>
            <div class="wpr-post-types-list">
                <?php foreach ( $post_types as $post_type ) :
                    $post_type_choice = ( isset( $post_type['choice'] ) && 'yes' === $post_type['choice'] ) ? 'yes' : 'no';
                    ?>
                    <div class="wpr-post-type-row wpr-auto-translation-post-type-row" data-post-type="<?php echo esc_attr( $post_type['name'] ); ?>">
                        <div class="wpr-post-type-label wpr-auto-translation-post-type-label">
                            <?php echo esc_html( $post_type['label'] ); ?>
                        </div>
                        <div class="wpr-post-type-actions wpr-auto-translation-post-type-actions">
                            <label>
                                <input type="radio" name="wpr_translate_post_types[<?php echo esc_attr( $post_type['name'] ); ?>]" value="yes" <?php checked( 'yes', $post_type_choice ); ?> />
                                <span><?php esc_html_e( 'Translate', 'wpr-translate' ); ?></span>
                            </label>
                            <label>
                                <input type="radio" name="wpr_translate_post_types[<?php echo esc_attr( $post_type['name'] ); ?>]" value="no" <?php checked( 'no', $post_type_choice ); ?> />
                                <span><?php esc_html_e( 'Do Not Translate', 'wpr-translate' ); ?></span>
                            </label>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php else : ?>
            <div class="wpr-post-types-empty">
                <?php esc_html_e( 'No post types available.', 'wpr-translate' ); ?>
            </div>
        <?php endif; ?>

        <div id="wpr_translate_auto_status_update" class="wpr-translate-auto-status"></div>
        <div class="wpr-settings-actions wpr-auto-translation-actions">
            <button type="button" class="button button-primary wpr-start-translation" data-label="<?php esc_attr_e( 'Start translation', 'wpr-translate' ); ?>"><?php esc_html_e( 'Start translation', 'wpr-translate' ); ?></button>
            <button type="button" class="button wpr-stop-translation" data-label="<?php esc_attr_e( 'Stop translation', 'wpr-translate' ); ?>"><?php esc_html_e( 'Stop translation', 'wpr-translate' ); ?></button>
        </div>
    </div>
</div>
