/*!
 * Auto Blog Writer - Manual Blog Generator and Countdown Script
 * @version 1.0.0
 * @author SRH Web Agency
 * @license GPL-2.0-or-later
 *
 * This file is part of Auto Blog Writer, a WordPress plugin.
 * 
 * Auto Blog Writer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 * 
 * Auto Blog Writer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this plugin. If not, see <https://www.gnu.org/licenses/gpl-2.0.html/>.
 */


document.addEventListener('DOMContentLoaded', function () {
    const acwbsrh_button = document.getElementById('acwbsrh-manual-generate');
    const acwbsrh_resultBox = document.getElementById('acwbsrh-manual-response');

    acwbsrh_button?.addEventListener('click', function () {
        acwbsrh_button.disabled = true;
        acwbsrh_resultBox.innerHTML = "⏳ Generating blog... Please wait.";

        fetch(acwbsrh_ajax.ajax_url, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            body: 'action=acwbsrh_start_auto_blogging'
        })
        .then(res => res.json())
        .then(data => {
            acwbsrh_button.disabled = false;

            if (data.success) {
                acwbsrh_resultBox.innerHTML = `<span style="color:green;">✅ Blog generated successfully! <a href="${acwbsrh_siteurl.ajax_url}/wp-admin/post.php?post=${data.data.post_id}&action=edit">Edit Post</a></span>`;
            } else {
                acwbsrh_resultBox.innerHTML = `<span style="color:red;">❌ ${data.data}</span>`;
            }
        })
        .catch(err => {
            acwbsrh_button.disabled = false;
            acwbsrh_resultBox.innerHTML = '<span style="color:red;">❌ An error occurred. Please try again.</span>';
            console.error(err);
        });
    });
});


// Countdown Timer for Next Scheduled Blog
function startCountdown() {
    const acwbsrh_timeElement = document.getElementById('acwbsrh-countdown-time');
    const acwbsrh_display = document.getElementById('acwbsrh-countdown-display');

    if (!acwbsrh_timeElement || !acwbsrh_display) return;

    const targetTime = new Date(acwbsrh_timeElement.dataset.time);

    function updateCountdown() {
        const now = new Date();
        const diff = targetTime - now;

        if (diff <= 0) {
            acwbsrh_display.textContent = '📝 Blog is publishing now...';
            clearInterval(interval);
            return;
        }

        const totalSeconds = Math.floor(diff / 1000);
        const days = Math.floor(totalSeconds / (3600 * 24));
        const hours = Math.floor((totalSeconds % (3600 * 24)) / 3600);
        const minutes = Math.floor((totalSeconds % 3600) / 60);
        const seconds = totalSeconds % 60;

        acwbsrh_display.textContent = `⏳ Time remaining: ${days}d ${hours}h ${minutes}m ${seconds}s`;
    }

    updateCountdown();
    const interval = setInterval(updateCountdown, 1000);
}

document.addEventListener('DOMContentLoaded', startCountdown);




