<?php
$ai_gemini_key        = $this->get_option('ai_gemini_pro_api_key');
$ai_chatgpt_key       = $this->get_option('ai_chatgpt_api_key');
$ai_copilot_key       = $this->get_option('ai_copilot_pro_api_key');
$ai_deepseek_key      = $this->get_option('ai_deepseek_pro_api_key');
$disable_ai_assistant = $this->get_option('disable_ai_assistant');
$disable_ai_rewrite   = $this->get_option('disable_ai_rewrite');
$ai_override_prompts  = $this->get_option('ai_override_prompts');
$ai_attach_context_default = $this->get_option('ai_attach_context_default');
$ai_defaults          = $this->get_ai_default_commands();

if ($ai_attach_context_default !== 'disabled') {
    $ai_attach_context_default = 'enabled';
}

$ai_command_rewrite = $this->get_option('ai_command_rewrite');
if ($ai_command_rewrite === false) {
    $ai_command_rewrite = $ai_defaults['rewrite_system'];
}

$ai_command_generate = $this->get_option('ai_command_generate');
if ($ai_command_generate === false) {
    $ai_command_generate = $ai_defaults['generate_system'];
}

$ai_command_rewrite_selected = $this->get_option('ai_command_rewrite_selected');
if ($ai_command_rewrite_selected === false) {
    $ai_command_rewrite_selected = $ai_defaults['rewrite_selected_template'];
}

$ai_command_existing_content = $this->get_option('ai_command_existing_content');
if ($ai_command_existing_content === false) {
    $ai_command_existing_content = $ai_defaults['existing_content_template'];
}

$override_enabled = ($ai_override_prompts === 'Y');
?>
<table class="form-table">
    <tbody>
        <tr>
            <th>
                <label for="disable_ai_assistant"><?php _e('Disable AI Assistant', 'wp-mailinglist'); ?></label>
                <?php echo $Html->help(__('Hide the AI Assistant button used for generating new content in the Create Newsletter screen.', 'wp-mailinglist')); ?>
            </th>
            <td>
                <label>
                    <input <?php checked($disable_ai_assistant, 'Y'); ?> type="checkbox" name="disable_ai_assistant" value="Y" id="disable_ai_assistant" />
                    <?php _e('Disable the AI Assistant button on the Create Newsletter page', 'wp-mailinglist'); ?>
                </label>
            </td>
        </tr>
        <tr>
            <th>
                <label for="disable_ai_rewrite"><?php _e('Disable AI Rewrite', 'wp-mailinglist'); ?></label>
                <?php echo $Html->help(__('Hide the AI Rewrite button that reworks highlighted text within the Create Newsletter editor.', 'wp-mailinglist')); ?>
            </th>
            <td>
                <label>
                    <input <?php checked($disable_ai_rewrite, 'Y'); ?> type="checkbox" name="disable_ai_rewrite" value="Y" id="disable_ai_rewrite" />
                    <?php _e('Disable the AI Rewrite button on the Create Newsletter page', 'wp-mailinglist'); ?>
                </label>
            </td>
        </tr>
        <tr>
            <th>
                <label><?php _e('Attach Context Default State', 'wp-mailinglist'); ?></label>
                <?php echo $Html->help(__('Attach context attaches the current editor content so the AI can better understand the context. Enabling this ensures that it\'s always ticked by default, and vice versa. Even if disabled, you can still tick the box to enable it each time when generating/rewriting text. This is helpful if you find yourself always disabling or enabling the Attach context option.', 'wp-mailinglist')); ?>
            </th>
            <td>
                <label><input type="radio" name="ai_attach_context_default" value="enabled" <?php checked($ai_attach_context_default, 'enabled'); ?> /> <?php _e('Enabled', 'wp-mailinglist'); ?></label>
                <label style="margin-left: 10px;"><input type="radio" name="ai_attach_context_default" value="disabled" <?php checked($ai_attach_context_default, 'disabled'); ?> /> <?php _e('Disabled', 'wp-mailinglist'); ?></label>
            </td>
        </tr>
        <tr>
            <th><label for="ai_gemini_pro_api_key"><?php _e('Gemini API Key (Both Free & Pro)', 'wp-mailinglist'); ?></label></th>
            <td>
                <input type="text" id="ai_gemini_pro_api_key" name="ai_gemini_pro_api_key" value="<?php echo esc_attr($ai_gemini_key); ?>" class="regular-text" />
                <p class="description">
                    <?php printf(__('Create your key %shere%s.', 'wp-mailinglist'), '<a href="https://aistudio.google.com/app/apikey" target="_blank">', '</a>'); ?>
                </p>
            </td>
        </tr>
        <tr>
            <th><label for="ai_chatgpt_api_key"><?php _e('ChatGPT API Key', 'wp-mailinglist'); ?></label></th>
            <td>
                <input type="text" id="ai_chatgpt_api_key" name="ai_chatgpt_api_key" value="<?php echo esc_attr($ai_chatgpt_key); ?>" class="regular-text" />
                <p class="description">
                    <?php printf(__('Create your key %shere%s.', 'wp-mailinglist'), '<a href="https://platform.openai.com/api-keys" target="_blank">', '</a>'); ?>
                </p>
                
            </td>
        </tr>
        <tr>
            <th><label for="ai_deepseek_pro_api_key"><?php _e('Deepseek API Key', 'wp-mailinglist'); ?></label></th>
            <td>
                <input type="text" id="ai_deepseek_pro_api_key" name="ai_deepseek_pro_api_key" value="<?php echo esc_attr($ai_deepseek_key); ?>" class="regular-text" />
                <p class="description">
                    <?php printf(__('Create your key %shere%s.', 'wp-mailinglist'), '<a href="https://platform.deepseek.com/api_keys" target="_blank">', '</a>'); ?>
                </p>
            </td>
            
        </tr>
        <tr>
            <th>
                <label for="ai_override_prompts"><?php _e('Override AI Commands', 'wp-mailinglist'); ?></label>
                <?php echo $Html->help(__('When unchecked, the default commands will be used. This lets you customize our default commands for the AI system, allowing you to generate and rewrite using your own instructions. Feel free to experiment. You can restore the default commands by clicking the button below.', 'wp-mailinglist')); ?>
            </th>
            <td>
                <label>
                    <input type="checkbox" id="ai_override_prompts" name="ai_override_prompts" value="Y" <?php checked($override_enabled); ?> />
                    <?php _e('Customize AI system commands and prompt templates', 'wp-mailinglist'); ?>
                </label>
            </td>
        </tr>
        <tr class="ai-command-fields" <?php echo $override_enabled ? '' : 'style="display:none;"'; ?>>
            <th>
                <label for="ai_command_generate"><?php _e('Generate System Command', 'wp-mailinglist'); ?></label>
                <?php echo $Html->help(sprintf(__('Used as the system message for generation requests.%1$sDefault: %2$s', 'wp-mailinglist'), '<br /><br />', '<code>' . nl2br(esc_html($ai_defaults['generate_system'])) . '</code>')); ?>
            </th>
            <td>
                <textarea id="ai_command_generate" name="ai_command_generate" class="large-text" rows="4"><?php echo esc_textarea($ai_command_generate); ?></textarea>
            </td>
        </tr>
        <tr class="ai-command-fields" <?php echo $override_enabled ? '' : 'style="display:none;"'; ?>>
            <th>
                <label for="ai_command_rewrite"><?php _e('Rewrite System Command', 'wp-mailinglist'); ?></label>
                <?php echo $Html->help(sprintf(__('Used as the system message for rewrite requests.%1$sDefault: %2$s', 'wp-mailinglist'), '<br /><br />', '<code>' . nl2br(esc_html($ai_defaults['rewrite_system'])) . '</code>')); ?>
            </th>
            <td>
                <textarea id="ai_command_rewrite" name="ai_command_rewrite" class="large-text" rows="4"><?php echo esc_textarea($ai_command_rewrite); ?></textarea>
            </td>
        </tr>
        <tr class="ai-command-fields" <?php echo $override_enabled ? '' : 'style="display:none;"'; ?>>
            <th>
                <label for="ai_command_existing_content"><?php _e('Existing Content Template', 'wp-mailinglist'); ?></label>
                <?php echo $Html->help(sprintf(__('Used when improving the editor content. Include %1$s and %2$s where the existing content and prompt should appear.%3$sDefault: %4$s', 'wp-mailinglist'), '<code>%editor%</code>', '<code>%prompt%</code>', '<br /><br />', '<code>' . nl2br(esc_html($ai_defaults['existing_content_template'])) . '</code>')); ?>
            </th>
            <td>
                <textarea id="ai_command_existing_content" name="ai_command_existing_content" class="large-text" rows="4"><?php echo esc_textarea($ai_command_existing_content); ?></textarea>
            </td>
        </tr>
        <tr class="ai-command-fields" <?php echo $override_enabled ? '' : 'style="display:none;"'; ?>>
            <th>
                <label for="ai_command_rewrite_selected"><?php _e('Rewrite Selection Template', 'wp-mailinglist'); ?></label>
                <?php echo $Html->help(sprintf(__('Used when rewriting highlighted text. Include %1$s and %2$s where the selected text and prompt should appear.%3$sDefault: %4$s', 'wp-mailinglist'), '<code>%selected%</code>', '<code>%prompt%</code>', '<br /><br />', '<code>' . nl2br(esc_html($ai_defaults['rewrite_selected_template'])) . '</code>')); ?>
            </th>
            <td>
                <textarea id="ai_command_rewrite_selected" name="ai_command_rewrite_selected" class="large-text" rows="4"><?php echo esc_textarea($ai_command_rewrite_selected); ?></textarea>
                <p>
                    <button type="submit" name="reset_ai_prompts" value="1" class="button"><?php _e('Reset commands to default', 'wp-mailinglist'); ?></button>
                </p>
            </td>
        </tr>
    </tbody>
</table>
<script type="text/javascript">
jQuery(document).ready(function($) {
    var $toggle = $('#ai_override_prompts');
    var $fields = $('.ai-command-fields');

    function newslettersToggleAiCommands() {
        if ($toggle.is(':checked')) {
            $fields.show();
        } else {
            $fields.hide();
        }
    }

    newslettersToggleAiCommands();
    $toggle.on('change', newslettersToggleAiCommands);
});
</script>