/**
 * Live Support Chat – Admin JS
 * Handles admin dashboard interactions: status toggle, chat view modal, delete.
 */
/* global lscAdmin, jQuery */
(function ($) {
    'use strict';

    $(function () {
        // Status toggle buttons.
        $(document).on('click', '#lsc-set-online, #lsc-set-offline', function () {
            var $btn   = $(this);
            var status = $btn.data('status');

            $.post(lscAdmin.ajaxUrl, {
                action: 'lsc_toggle_status',
                nonce:  lscAdmin.nonce,
                status: status,
            }, function (res) {
                if (!res.success) { return; }

                var isOnline = 'online' === status;

                // Update indicator.
                $('#lsc-status-indicator')
                    .removeClass('lsc-indicator-online lsc-indicator-offline')
                    .addClass(isOnline ? 'lsc-indicator-online' : 'lsc-indicator-offline');

                // Update label.
                $('#lsc-status-label').text(isOnline ? lscAdmin.i18n.online : lscAdmin.i18n.offline);

                // Toggle button states.
                $('#lsc-set-online').prop('disabled',  isOnline).toggleClass('button-disabled',  isOnline);
                $('#lsc-set-offline').prop('disabled', !isOnline).toggleClass('button-disabled', !isOnline);
            });
        });

        // View chat messages (modal).
        $(document).on('click', '.lsc-view-chat-btn', function () {
            var chatId = $(this).data('chat-id');
            loadChatModal(chatId);
        });

        $('#lsc-modal-close').on('click', function () {
            $('#lsc-chat-modal').hide();
        });

        // Close modal on overlay click.
        $(document).on('click', function (e) {
            if ($(e.target).is('#lsc-chat-modal')) {
                $('#lsc-chat-modal').hide();
            }
        });

        // Delete chat.
        $(document).on('click', '.lsc-delete-chat-btn', function () {
            if (!window.confirm(lscAdmin.i18n.confirmDelete)) { return; }

            var $btn   = $(this);
            var chatId = $btn.data('chat-id');

            $.post(lscAdmin.ajaxUrl, {
                action:  'lsc_delete_chat',
                nonce:   lscAdmin.nonce,
                chat_id: chatId,
            }, function (res) {
                if (res.success) {
                    $btn.closest('tr').fadeOut(300, function () { $(this).remove(); });
                }
            });
        });
    });

    /* ── Load chat messages into modal ── */
    function loadChatModal(chatId) {
        var $modal    = $('#lsc-chat-modal');
        var $messages = $('#lsc-modal-messages');

        $messages.html('<p style="text-align:center;color:#94a3b8;">Loading…</p>');
        $('#lsc-modal-title').text('Chat #' + chatId);
        $modal.show();

        // Use chatter poll with chat_id to get messages.
        $.post(lscAdmin.ajaxUrl, {
            action:   'lsc_chatter_poll',
            nonce:    lscAdmin.chatterNonce,
            chat_id:  chatId,
            since_id: 0,
        }, function (res) {
            $messages.empty();
            if (!res.success) {
                $messages.html('<p style="color:#ef4444;">Could not load messages.</p>');
                return;
            }

            var msgs = res.data.messages || [];
            if (!msgs.length) {
                $messages.html('<p style="color:#94a3b8;text-align:center;">No messages yet.</p>');
                return;
            }

            $.each(msgs, function (i, m) {
                var $msg = $('<div class="lsc-modal-msg"></div>').addClass(m.sender).text(m.content);
                var who  = 'chatter' === m.sender ? 'Support' : 'Visitor';
                $msg.append($('<div class="lsc-modal-msg-meta"></div>').text(who + ' · ' + m.time));
                $messages.append($msg);
            });

            $messages.scrollTop($messages[0].scrollHeight);
        });
    }

}(jQuery));
