<?php
/**
 * Word Replacement & Pronunciation Control page
 *
 * @package    TTSWP
 * @subpackage Mementor_TTS/admin/partials/pages
 */

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

if (!current_user_can('manage_options')) {
    wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'text-to-speech-tts'));
}

$is_pro_active = ttswp_has_paid_plan();
$has_pro_license = $is_pro_active;

$tts_page_title = __('Word Replacement & Pronunciation Controls', 'text-to-speech-tts');
$tts_topbar_actions = '';
if (!$has_pro_license) {
    $tts_topbar_actions = '<a href="' . esc_url(admin_url('admin.php?page=text-to-speech-tts-upgrade')) . '" class="tts-btn tts-btn-primary">' . esc_html__('Upgrade to PRO', 'text-to-speech-tts') . '</a>';
}
$tts_requires_connection = true; // gate page behind SaaS connection

include MEMENTOR_TTS_PLUGIN_DIR . 'admin/partials/layout-open.php';
?>
<?php include(MEMENTOR_TTS_PLUGIN_DIR . 'admin/partials/navigation.php'); ?>

        <div class="tts-card tts-card-inset tts-word-replacement-card">
            <div class="tts-card-header tts-card-header-inline">
                <h3 class="tts-card-title">
                    <?php esc_html_e('Word Replacement & Pronunciation Control', 'text-to-speech-tts'); ?>
                    <?php if (!$has_pro_license): ?>
                        <span class="tts-pro-badge"><?php esc_html_e('PRO', 'text-to-speech-tts'); ?></span>
                    <?php endif; ?>
                </h3>
                <p class="tts-card-desc"><?php esc_html_e('Replace words or phrases before text-to-speech conversion.', 'text-to-speech-tts'); ?></p>
            </div>
                <div class="<?php echo (!$has_pro_license) ? 'tts-content-pro-locked' : ''; ?>">
                    <table class="wp-list-table widefat striped" id="word-replacements-table">
                        <thead>
                            <tr>
                                <th><?php esc_html_e('Original Word/Phrase', 'text-to-speech-tts'); ?></th>
                                <th><?php esc_html_e('Replace With', 'text-to-speech-tts'); ?></th>
                                <th><?php esc_html_e('Actions', 'text-to-speech-tts'); ?></th>
                            </tr>
                        </thead>
                        <tbody id="word-replacements-list">
                            <?php
                            if (!$has_pro_license) {
                                ?>
                                <tr>
                                    <td>UN</td>
                                    <td>United Nations</td>
                                    <td>
                                        <button class="tts-btn" disabled><?php esc_html_e('Edit', 'text-to-speech-tts'); ?></button>
                                        <button class="tts-btn tts-btn-danger" disabled><?php esc_html_e('Delete', 'text-to-speech-tts'); ?></button>
                                    </td>
                                </tr>
                                <tr>
                                    <td>CEO</td>
                                    <td>Chief Executive Officer</td>
                                    <td>
                                        <button class="tts-btn" disabled><?php esc_html_e('Edit', 'text-to-speech-tts'); ?></button>
                                        <button class="tts-btn tts-btn-danger" disabled><?php esc_html_e('Delete', 'text-to-speech-tts'); ?></button>
                                    </td>
                                </tr>
                                <?php
                            } else {
                                $replacements = get_option('mementor_tts_word_replacements', array());
                                if (!empty($replacements) && is_array($replacements)) {
                                    foreach ($replacements as $index => $replacement) {
                                        if (isset($replacement['original']) && isset($replacement['replacement'])) {
                                            ?>
                                            <tr data-index="<?php echo esc_attr($index); ?>">
                                                <td><?php echo esc_html($replacement['original']); ?></td>
                                                <td><?php echo esc_html($replacement['replacement']); ?></td>
                                                <td>
                                                    <button class="tts-btn edit-replacement" data-index="<?php echo esc_attr($index); ?>">
                                                        <?php esc_html_e('Edit', 'text-to-speech-tts'); ?>
                                                    </button>
                                                    <button class="tts-btn tts-btn-danger delete-replacement" data-index="<?php echo esc_attr($index); ?>">
                                                        <?php esc_html_e('Delete', 'text-to-speech-tts'); ?>
                                                    </button>
                                                </td>
                                            </tr>
                                            <?php
                                        }
                                    }
                                } else {
                                    ?>
                                    <tr class="no-replacements">
                                        <td colspan="3" class="tts-empty-table">
                                            <?php esc_html_e('No word replacements configured yet.', 'text-to-speech-tts'); ?>
                                        </td>
                                    </tr>
                                    <?php
                                }
                            }
                            ?>
                        </tbody>
                    </table>

                    <div class="tts-add-replacement">
                        <h4><?php esc_html_e('Add New Replacement', 'text-to-speech-tts'); ?></h4>
                        <form id="add-word-replacement-form">
                            <div class="tts-audio-grid" style="grid-template-columns: 1fr 1fr;">
                                <div class="tts-audio-field">
                                    <label class="tts-audio-label" for="new-original-word"><?php esc_html_e('Original Word/Phrase', 'text-to-speech-tts'); ?></label>
                                    <input type="text" id="new-original-word" placeholder="<?php esc_attr_e('e.g., UN', 'text-to-speech-tts'); ?>" <?php echo (!$has_pro_license) ? 'disabled' : ''; ?>>
                                </div>
                                <div class="tts-audio-field">
                                    <label class="tts-audio-label" for="new-replacement-word"><?php esc_html_e('Replace With', 'text-to-speech-tts'); ?></label>
                                    <input type="text" id="new-replacement-word" placeholder="<?php esc_attr_e('e.g., United Nations', 'text-to-speech-tts'); ?>" <?php echo (!$has_pro_license) ? 'disabled' : ''; ?>>
                                </div>
                            </div>
                            <div class="tts-content-submit">
                                <button type="button" id="add-word-replacement" class="tts-btn tts-btn-primary" <?php echo (!$has_pro_license) ? 'disabled' : ''; ?>>
                                    <?php esc_html_e('Add Replacement', 'text-to-speech-tts'); ?>
                                </button>
                            </div>
                        </form>
                    </div>

                    <div class="tts-replacement-examples">
                        <div class="tts-examples-grid">
                            <div class="tts-example-section">
                                <h4><?php esc_html_e('Common Examples:', 'text-to-speech-tts'); ?></h4>
                                <ul>
                                    <li>UN &rarr; United Nations</li>
                                    <li>USA &rarr; United States of America</li>
                                    <li>CEO &rarr; Chief Executive Officer</li>
                                    <li>FAQ &rarr; Frequently Asked Questions</li>
                                    <li>$ &rarr; dollars</li>
                                    <li>% &rarr; percent</li>
                                </ul>
                            </div>
                            <div class="tts-example-section">
                                <h4><?php esc_html_e('Pronunciation Fine-Tuning:', 'text-to-speech-tts'); ?></h4>
                                <p class="tts-example-intro">
                                    <?php esc_html_e('Improve pronunciation of names or unusual words:', 'text-to-speech-tts'); ?>
                                </p>
                                <ul>
                                    <li>Claughton &rarr; Cloffton <em>(<?php esc_html_e('phonetic spelling', 'text-to-speech-tts'); ?>)</em></li>
                                    <li>Leicester &rarr; Lester <em>(<?php esc_html_e('British place name', 'text-to-speech-tts'); ?>)</em></li>
                                    <li>Siobhan &rarr; Shivawn <em>(<?php esc_html_e('Irish name', 'text-to-speech-tts'); ?>)</em></li>
                                    <li>Django &rarr; Jango <em>(<?php esc_html_e('silent D', 'text-to-speech-tts'); ?>)</em></li>
                                    <li>Nguyen &rarr; Win <em>(<?php esc_html_e('Vietnamese name', 'text-to-speech-tts'); ?>)</em></li>
                                    <li>Worcestershire &rarr; Wooster-sher <em>(<?php esc_html_e('British place name', 'text-to-speech-tts'); ?>)</em></li>
                                </ul>
                            </div>
                        </div>
                        <p class="tts-tip">
                            <em><?php esc_html_e('Tip: Test different phonetic spellings in the TTS Generator to find the best pronunciation for your specific words.', 'text-to-speech-tts'); ?></em>
                        </p>
                    </div>

                    <?php if (!$has_pro_license): ?>
                        <div class="tts-content-pro-notice">
                            <span><?php esc_html_e('Word Replacement is a PRO feature. Upgrade to customize how words and acronyms are pronounced.', 'text-to-speech-tts'); ?></span>
                            <a href="<?php echo esc_url(admin_url('admin.php?page=text-to-speech-tts-upgrade')); ?>" class="tts-btn tts-btn-primary">
                                <?php esc_html_e('Upgrade to PRO', 'text-to-speech-tts'); ?>
                            </a>
                        </div>
                    <?php endif; ?>
                </div>
        </div>

<?php include MEMENTOR_TTS_PLUGIN_DIR . 'admin/partials/layout-close.php'; ?>

<?php if ($has_pro_license): ?>
<script>
jQuery(document).ready(function($) {
    $('#add-word-replacement').on('click', function() {
        var original = $('#new-original-word').val().trim();
        var replacement = $('#new-replacement-word').val().trim();

        if (!original || !replacement) {
            alert('<?php esc_html_e('Please enter both original and replacement text.', 'text-to-speech-tts'); ?>');
            return;
        }

        var data = {
            action: 'mementor_tts_add_word_replacement',
            nonce: '<?php echo wp_create_nonce('mementor_tts_word_replacement'); ?>',
            original: original,
            replacement: replacement
        };

        $.post(ajaxurl, data, function(response) {
            if (response.success) {
                location.reload();
            } else {
                alert(response.data.message || '<?php esc_html_e('Error adding replacement.', 'text-to-speech-tts'); ?>');
            }
        });
    });

    $(document).on('click', '.delete-replacement', function() {
        if (!confirm('<?php esc_html_e('Are you sure you want to delete this replacement?', 'text-to-speech-tts'); ?>')) {
            return;
        }

        var index = $(this).data('index');
        var data = {
            action: 'mementor_tts_delete_word_replacement',
            nonce: '<?php echo wp_create_nonce('mementor_tts_word_replacement'); ?>',
            index: index
        };

        $.post(ajaxurl, data, function(response) {
            if (response.success) {
                location.reload();
            } else {
                alert(response.data.message || '<?php esc_html_e('Error deleting replacement.', 'text-to-speech-tts'); ?>');
            }
        });
    });

    $(document).on('click', '.edit-replacement', function() {
        var $row = $(this).closest('tr');
        var index = $(this).data('index');
        var original = $row.find('td:first').text();
        var replacement = $row.find('td:nth-child(2)').text();

        var editForm = '<td><input type="text" class="edit-original regular-text" value="' + original + '" /></td>' +
                      '<td><input type="text" class="edit-replacement-text regular-text" value="' + replacement + '" /></td>' +
                      '<td>' +
                          '<button class="tts-btn tts-btn-primary save-edit" data-index="' + index + '">Save</button> ' +
                          '<button class="tts-btn cancel-edit">Cancel</button>' +
                      '</td>';

        $row.html(editForm);
    });

    $(document).on('click', '.save-edit', function() {
        var index = $(this).data('index');
        var $row = $(this).closest('tr');
        var original = $row.find('.edit-original').val().trim();
        var replacement = $row.find('.edit-replacement-text').val().trim();

        if (!original || !replacement) {
            alert('<?php esc_html_e('Please enter both original and replacement text.', 'text-to-speech-tts'); ?>');
            return;
        }

        var data = {
            action: 'mementor_tts_update_word_replacement',
            nonce: '<?php echo wp_create_nonce('mementor_tts_word_replacement'); ?>',
            index: index,
            original: original,
            replacement: replacement
        };

        $.post(ajaxurl, data, function(response) {
            if (response.success) {
                location.reload();
            } else {
                alert(response.data.message || '<?php esc_html_e('Error updating replacement.', 'text-to-speech-tts'); ?>');
            }
        });
    });

    $(document).on('click', '.cancel-edit', function() {
        location.reload();
    });
});
</script>
<?php endif; ?>
