/**
 * MediaTasker — Admin Scripts
 *
 * Handles admin UI interactions for the plugin's wp-admin pages.
 *
 * @package MediaTasker
 */

/* global jQuery */

(function ($) {
    'use strict';

    $(document).ready(function () {

        /**
         * Copy Shortcode button handler.
         *
         * Copies the shortcode text to the clipboard and shows
         * a temporary "Copied!" confirmation.
         */
        $('.mediat_copy-shortcode').on('click', function () {
            var $button = $(this);
            var shortcode = $button.data('shortcode');
            var original = $button.text();

            if (!shortcode) {
                return;
            }

            navigator.clipboard.writeText(shortcode).then(function () {
                $button.text($button.data('copiedText') || 'Copied!');
                $button.addClass('mediat_copied');

                setTimeout(function () {
                    $button.text(original);
                    $button.removeClass('mediat_copied');
                }, 2000);
            });
        });

        /**
         * Work Log UI Handler
         */
        var $workLogFilterBtn = $('#mediat_log_filter_btn');
        if ($workLogFilterBtn.length) {
            var currentOffset = 0;
            var limit = 50;

            function fetchLogs(append) {
                var actionType = $('#mediat_log_action_type').val();
                var dateFrom = $('#mediat_log_date_from').val();
                var dateTo = $('#mediat_log_date_to').val();
                var userId = $('#mediat_log_user_id').length ? $('#mediat_log_user_id').val() : '';

                if (!append) {
                    currentOffset = 0;
                    $('#mediat_log_table_body').html('<tr><td colspan="5">Loading...</td></tr>');
                }

                $('.mediat_log-spinner').addClass('is-active');

                $.ajax({
                    url: ajaxurl,
                    type: 'POST',
                    data: {
                        action: 'mediat_fetch_work_logs',
                        nonce: mediat_admin.nonce,
                        action_type: actionType,
                        date_from: dateFrom,
                        date_to: dateTo,
                        user_id: userId,
                        offset: currentOffset,
                        limit: limit
                    },
                    success: function (res) {
                        $('.mediat_log-spinner').removeClass('is-active');
                        if (res.success) {
                            if (append && currentOffset > 0) {
                                $('#mediat_log_table_body').append(res.data.html);
                            } else {
                                $('#mediat_log_table_body').html(res.data.html);
                            }

                            if (res.data.has_more) {
                                $('#mediat_log_load_more').show();
                            } else {
                                $('#mediat_log_load_more').hide();
                            }
                        } else {
                            showError('Failed to fetch logs: ' + (res.data.message || 'Unknown error'));
                        }
                    },
                    error: function () {
                        $('.mediat_log-spinner').removeClass('is-active');
                        showError('Server error while fetching logs.');
                    }
                });
            }

            function showError(message) {
                var $container = $('#mediat_admin-notice-container');
                if ($container.length) {
                    $container.find('p').text(message);
                    $container.show();
                    // Scroll to top of the wrap so notice is visible.
                    $('html, body').animate({ scrollTop: $('.mediat_admin-wrap').offset().top - 40 }, 200);
                } else {
                    console.error(message);
                }
            }

            $('.notice-dismiss').on('click', function () {
                $(this).closest('.notice').hide();
            });

            $workLogFilterBtn.on('click', function () {
                fetchLogs(false);
            });

            $('#mediat_log_load_more').on('click', function () {
                currentOffset += limit;
                fetchLogs(true);
            });

            // Export to PDF/CSV handlers
            
        }

    });
})(jQuery);
