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

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

// Normalize settings data for the template.
$settings   = isset( $data['settings'] ) && is_array( $data['settings'] ) ? $data['settings'] : array();
$taxonomies = isset( $data['taxonomies'] ) && is_array( $data['taxonomies'] ) ? $data['taxonomies'] : array();
$defaults   = wp_parse_args(
    $settings,
    array(
        'default_language'        => '',
        'url_strategy'            => 'subdirectory',
        'sync_media'              => true,
        'elementor_compatibility' => true,
        'taxonomy_modes'          => array(),
        'taxonomy_auto_duplicate' => array(),
    )
);
$taxonomy_modes          = isset( $defaults['taxonomy_modes'] ) && is_array( $defaults['taxonomy_modes'] ) ? $defaults['taxonomy_modes'] : array();
$taxonomy_auto_duplicate = isset( $defaults['taxonomy_auto_duplicate'] ) && is_array( $defaults['taxonomy_auto_duplicate'] ) ? $defaults['taxonomy_auto_duplicate'] : array();
$rendered_taxonomies     = array();
?>
<div class="wrap wpr-translate-admin">
    <h1><?php esc_html_e( 'Taxonomy Translation', 'wpr-translate' ); ?></h1>

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

        <?php if ( empty( $taxonomies ) ) : ?>
            <p><?php esc_html_e( 'No public taxonomies are available for configuration.', 'wpr-translate' ); ?></p>
        <?php else : ?>
            <div class="wpr-taxonomy-table-wrapper">
                <h2 class="wpr-taxonomy-table-title"><?php esc_html_e( 'Taxonomy Translation', 'wpr-translate' ); ?></h2>
                <p class="description"><?php esc_html_e( 'Choose how each taxonomy should behave in translations. Translatable taxonomies only show terms in the current language on the front end, and Automation can auto-create missing term translations to keep content aligned.', 'wpr-translate' ); ?></p>
                <div class="wpr-taxonomy-table">
                    <div class="wpr-taxonomy-row wpr-taxonomy-header">
                        <div class="wpr-taxonomy-col wpr-taxonomy-col-title"><?php esc_html_e( 'Taxonomy', 'wpr-translate' ); ?></div>
                        <div class="wpr-taxonomy-col wpr-taxonomy-col-mode"><?php esc_html_e( 'Translation Mode', 'wpr-translate' ); ?></div>
                        <div class="wpr-taxonomy-col wpr-taxonomy-col-automation"><?php esc_html_e( 'Automation', 'wpr-translate' ); ?></div>
                    </div>
                    <?php foreach ( $taxonomies as $taxonomy ) :
                        $slug  = isset( $taxonomy['name'] ) ? sanitize_key( $taxonomy['name'] ) : '';
                        $label = isset( $taxonomy['label'] ) ? $taxonomy['label'] : $slug;

                        if ( '' === $slug ) {
                            continue;
                        }

                        $rendered_taxonomies[ $slug ] = true;

                        $current_mode      = isset( $taxonomy_modes[ $slug ] ) ? $taxonomy_modes[ $slug ] : 'translate';
                        if ( ! in_array( $current_mode, array( 'translate', 'not_translatable' ), true ) ) {
                            $current_mode = 'translate';
                        }
                        $auto_duplicate_on = ! empty( $taxonomy_auto_duplicate[ $slug ] );
                        ?>
                        <div class="wpr-taxonomy-row">
                            <div class="wpr-taxonomy-col wpr-taxonomy-col-title">
                                <div class="wpr-taxonomy-title"><?php echo esc_html( $label ); ?></div>
                                <div class="wpr-taxonomy-slug"><?php echo esc_html( $slug ); ?></div>
                            </div>
                            <div class="wpr-taxonomy-col wpr-taxonomy-col-mode wpr-taxonomy-options">
                                <label>
                                    <input type="radio" name="wpr_translate_settings[taxonomy_modes][<?php echo esc_attr( $slug ); ?>]" value="translate" <?php checked( $current_mode, 'translate' ); ?> />
                                    <span><?php esc_html_e( 'Translatable', 'wpr-translate' ); ?></span>
                                </label>
                                <label>
                                    <input type="radio" name="wpr_translate_settings[taxonomy_modes][<?php echo esc_attr( $slug ); ?>]" value="not_translatable" <?php checked( $current_mode, 'not_translatable' ); ?> />
                                    <span><?php esc_html_e( 'Not translatable', 'wpr-translate' ); ?></span>
                                </label>
                            </div>
                            <div class="wpr-taxonomy-col wpr-taxonomy-col-automation wpr-taxonomy-automation">
                                <label>
                                    <input type="checkbox" name="wpr_translate_settings[taxonomy_auto_duplicate][<?php echo esc_attr( $slug ); ?>]" value="1" <?php checked( $auto_duplicate_on ); ?> />
                                    <span><?php esc_html_e( 'Automatically duplicate missing translations', 'wpr-translate' ); ?></span>
                                </label>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
        <?php endif; ?>

        <?php
        // Preserve taxonomy preferences that are not currently rendered.
        if ( ! empty( $taxonomy_modes ) ) {
            foreach ( $taxonomy_modes as $taxonomy_slug => $mode ) {
                $taxonomy_slug = sanitize_key( $taxonomy_slug );

                if ( '' === $taxonomy_slug || isset( $rendered_taxonomies[ $taxonomy_slug ] ) ) {
                    continue;
                }
                ?>
                <input type="hidden" name="wpr_translate_settings[taxonomy_modes][<?php echo esc_attr( $taxonomy_slug ); ?>]" value="<?php echo esc_attr( sanitize_key( $mode ) ); ?>" />
                <?php
            }
        }

        if ( ! empty( $taxonomy_auto_duplicate ) ) {
            foreach ( $taxonomy_auto_duplicate as $taxonomy_slug => $enabled ) {
                $taxonomy_slug = sanitize_key( $taxonomy_slug );

                if ( '' === $taxonomy_slug || isset( $rendered_taxonomies[ $taxonomy_slug ] ) || empty( $enabled ) ) {
                    continue;
                }
                ?>
                <input type="hidden" name="wpr_translate_settings[taxonomy_auto_duplicate][<?php echo esc_attr( $taxonomy_slug ); ?>]" value="1" />
                <?php
            }
        }
        ?>

        <input type="hidden" name="wpr_translate_settings[default_language]" value="<?php echo esc_attr( $defaults['default_language'] ); ?>" />
        <input type="hidden" name="wpr_translate_settings[url_strategy]" value="<?php echo esc_attr( $defaults['url_strategy'] ); ?>" />
        <input type="hidden" name="wpr_translate_settings[sync_media]" value="<?php echo ! empty( $defaults['sync_media'] ) ? '1' : ''; ?>" />
        <input type="hidden" name="wpr_translate_settings[elementor_compatibility]" value="<?php echo ! empty( $defaults['elementor_compatibility'] ) ? '1' : ''; ?>" />

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