/*
Nuclear Fancy Pants Edition™ — NUCLEAR+++
File: assets/js/amai-admin-modules.js
Last Edited: 2026-03-02
Version: 1.0.65
Description: JavaScript for AI provider module cards - modal toggle for activation (API key) and model change, outside click close, auto-focus/select on pre-filled prefix
*/
document.addEventListener('DOMContentLoaded', function() {
    // Open activation modal (API key entry)
    document.querySelectorAll('.amai-activate-modal-btn').forEach(button => {
        button.addEventListener('click', function(e) {
            e.preventDefault();
            const providerId = this.getAttribute('data-provider-id');
            const modalId = 'activate-modal-' + providerId;
            const modal = document.getElementById(modalId);
            if (modal) {
                modal.style.display = 'flex';
                // Auto-focus and select the API key input (prefix already pre-filled by PHP)
                const apiInput = modal.querySelector('input[name="api_key"]');
                if (apiInput) {
                    setTimeout(() => {
                        apiInput.focus();
                        apiInput.select();
                    }, 100);
                }
            }
        });
    });
    // Open change model modal
    document.querySelectorAll('.amai-change-model').forEach(link => {
        link.addEventListener('click', function(e) {
            e.preventDefault();
            const providerId = this.getAttribute('data-provider-id');
            const modalId = 'modal-model-' + providerId;
            const modal = document.getElementById(modalId);
            if (modal) {
                modal.style.display = 'flex';
            }
        });
    });
    // Close any modal (click on Cancel button or outside modal content)
    document.querySelectorAll('.close-modal, .amai-modal').forEach(el => {
        el.addEventListener('click', function(e) {
            if (e.target.classList.contains('amai-modal') || e.target.classList.contains('close-modal')) {
                const modal = e.target.closest('.amai-modal');
                if (modal) {
                    modal.style.display = 'none';
                }
            }
        });
    });
});