<?php

/**
 * OnPage Notifications List View
 *
 * Renders the admin list page for OnPage Notifications with filtering, searching,
 * and bulk actions capabilities. Includes pre-created notifications archive and import functionality.
 *
 * @since 2.0.0
 * @author Hossein <hossein@notifal.com>
 * @package Notifal\Modules\OnPageNotification\Presentation\Admin\Views
 */

use Notifal\Infrastructure\WordPress\Hooks\ActionHooks;
use Notifal\Shared\AdminUI\Lists\BaseListView;
use Notifal\Shared\AdminUI\Toast\ToastRenderer;
use Notifal\Infrastructure\WordPress\Admin\Helpers\AdminStatsService;
use Notifal\Modules\OnPageNotification\Application\Services\Utility\UrlService;
use Notifal\Shared\Services\NotifalIconService;

if (!defined('ABSPATH')) exit;

// Initialize services using dependency injection
$adminStatsService = notifal_app(AdminStatsService::class);
$urlService        = notifal_app(UrlService::class);

// Get data for rendering
$notification_total = $adminStatsService->getTotalPosts('notifal_onpage_notif');
$status_tabs        = $adminStatsService->getStatusTabs('notifal_onpage_notif');
$add_new_url        = $urlService->getCreateNotificationUrl();

/**
 * Fires before rendering OnPage Notifications list admin page.
 *
 * Allows developers to add custom content or modify data before the notifications list is rendered.
 *
 * @since 2.0.0
 * @param int $notification_total Total number of notifications
 * @param array $status_tabs Status tab data for filtering
 * @param string $add_new_url URL for creating new notifications
 */
do_action(ActionHooks::ADMIN_ONPAGE_NOTIFICATIONS_BEFORE, $notification_total, $status_tabs, $add_new_url);
?>

<div class="wrap notifal-admin-page">
    <?php do_action(ActionHooks::ADMIN_PAGE_CONTENT_BEFORE); ?>
    <h1></h1>

    <div class="notification-list-parent notifal-mt-20">
    <?php
        $listView = new BaseListView([
            'title'              => __('OnPage Notifications', 'notifal'),
            'post_type'          => 'notifal_onpage_notif',
            'add_new_url'        => esc_url($add_new_url),
            'search_placeholder' => __('Search notifications...', 'notifal'),
            'columns'            => [
                'title'    => __('Title', 'notifal'),
                'enabled'  => __('Status', 'notifal'),
                'labels'   => __('Labels', 'notifal'),
                'date'     => __('Date', 'notifal'),
            ],
            'per_page'     => 15,
            'bulk_actions' => [
                'duplicate' => __('Duplicate', 'notifal'),
                'export' => __('Export', 'notifal'),
                'delete' => __('Move to Trash', 'notifal'),
            ],
            'status_tabs'  => $status_tabs,
            'bulk_actions_handled' => true, // Bulk actions handled by MenuController
        ]);

        // Add custom header actions hook for pre-created notifications modal button
        add_action('notifal_list_header_actions', function() {
            echo '<button type="button" class="notifal-button secondary notifal-flex notifal-gap-10" id="notifal-explore-precreated-btn">'
                . NotifalIconService::render('lightbulb', 20)
                . esc_html__('Explore Pre-created Notifications', 'notifal')
                . '</button>';
        });

        $listView->render();
        ?>

        <!-- Pre-created Notifications Archive Container (loaded asynchronously to avoid blocking page) -->
        <div id="notifal-precreated-archive-container" class="notifal-precreated-archive-container" data-notifal-archive-async="1">
            <div class="notifal-precreated-archive-loading" aria-live="polite" role="status" aria-label="<?php esc_attr_e('Loading pre-created notifications...', 'notifal'); ?>">
                <div class="notifal-precreated-archive-loading-spinner" aria-hidden="true"></div>
                <p class="notifal-precreated-archive-loading-text"><?php esc_html_e('Loading pre-created notifications...', 'notifal'); ?></p>
            </div>
        </div>
    </div>
</div>

<!-- Pre-created Notifications Modal -->
<?php include_once __DIR__ . '/components/precreated-notifications-modal.php'; ?>

<!-- OnPage AI Prompt Modal -->
<?php include_once __DIR__ . '/components/onpage-ai-prompt-modal.php'; ?>

<!-- Import Modal -->
<div class="notifal-modal-backdrop" id="notifal-import-modal">
    <div class="notifal-modal">
        <div class="notifal-modal-header">
            <h2><?php esc_html_e('Import OnPage Notifications', 'notifal'); ?></h2>
            <button type="button" class="notifal-modal-close" aria-label="<?php esc_attr_e('Close', 'notifal'); ?>"><span class="notifal-icon notifal-icon-x-circle size-16"></span></button>
        </div>

        <div class="notifal-modal-body">
            <p class="notifal-text-center notifal-text-muted">
                <?php esc_html_e('Import notifications from a JSON or ZIP file, or paste exported JSON directly. Only use data from a trusted Notifal export.', 'notifal'); ?>
            </p>

            <form id="notifal-import-form" enctype="multipart/form-data" class="notifal-mt-20">
                <?php
                $import_nonce = wp_create_nonce('notifal_import_onpage_notification_ajax_nonce');
                wp_nonce_field('notifal_import_onpage_notification_ajax_nonce', '_wpnonce', false);
                ?>
                <input type="hidden" name="action" value="notifal_import_onpage_notification_ajax">
                <input type="hidden" name="notifal_import_source" id="notifal_import_source" value="file">

                <div class="notifal-import-mode-switch" role="tablist" aria-label="<?php esc_attr_e('Import method', 'notifal'); ?>">
                    <button type="button"
                            class="notifal-import-mode-btn is-active"
                            id="notifal-import-mode-file"
                            role="tab"
                            aria-selected="true"
                            aria-controls="notifal-import-panel-file"
                            data-notifal-import-mode="file">
                        <?php esc_html_e('Upload file', 'notifal'); ?>
                    </button>
                    <button type="button"
                            class="notifal-import-mode-btn"
                            id="notifal-import-mode-json"
                            role="tab"
                            aria-selected="false"
                            aria-controls="notifal-import-panel-json"
                            data-notifal-import-mode="json">
                        <?php esc_html_e('Paste JSON', 'notifal'); ?>
                    </button>
                </div>

                <div class="notifal-import-panel" id="notifal-import-panel-file" role="tabpanel" aria-labelledby="notifal-import-mode-file">
                    <div class="notifal-form-group">
                        <div class="notifal-file-upload-area" id="notifal-file-upload-area">
                            <div class="notifal-file-upload-icon">
                                <?php echo NotifalIconService::render('cloud-arrow-up', 48); ?>
                            </div>
                            <div class="notifal-file-upload-text">
                                <span class="notifal-file-upload-title"><?php esc_html_e('Drop your file here', 'notifal'); ?></span>
                                <span class="notifal-file-upload-subtitle"><?php esc_html_e('or click to browse', 'notifal'); ?></span>
                            </div>
                            <div class="notifal-file-upload-formats">
                                <?php esc_html_e('Supported formats: JSON, ZIP (max 10MB)', 'notifal'); ?>
                            </div>
                            <input type="file"
                                   id="notifal_import_file"
                                   name="notifal_import_file"
                                   accept=".json,.zip"
                                   class="notifal-file-input">
                        </div>

                        <div class="notifal-file-info notifal-hidden" id="notifal-file-info">
                            <div class="notifal-file-info-content">
                                <span class="notifal-file-info-icon">
                                    <?php echo NotifalIconService::render('file-earmark1', 20); ?>
                                </span>
                                <span class="notifal-file-info-name" id="notifal-file-name"></span>
                                <button type="button" class="notifal-file-remove" id="notifal-file-remove" aria-label="<?php esc_attr_e('Remove file', 'notifal'); ?>">
                                    <?php echo NotifalIconService::render('x-circle', 16); ?>
                                </button>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="notifal-import-panel notifal-hidden" id="notifal-import-panel-json" role="tabpanel" aria-labelledby="notifal-import-mode-json">
                    <div class="notifal-form-group">
                        <div class="notifal-alert notifal-info notifal-import-ai-bridge-notice notifal-hidden" id="notifal-import-ai-bridge-notice" role="status">
                            <p><?php esc_html_e('Paste the full notification JSON from your AI assistant below. Confirm it is from a trusted source, then import. The notification will be saved as a disabled draft for your review.', 'notifal'); ?></p>
                            <div class="notifal-import-ai-bridge-actions">
                                <button type="button" class="notifal-button secondary" id="notifal-import-ai-bridge-dismiss">
                                    <?php esc_html_e('Dismiss', 'notifal'); ?>
                                </button>
                            </div>
                        </div>

                        <div class="notifal-alert notifal-info notifal-import-security-notice" role="note">
                            <p><?php esc_html_e('Security notice: Only paste JSON that you exported from a trusted reference. Untrusted data may contain harmful scripts or settings. Imported notifications are saved as disabled drafts so you can review them before publishing.', 'notifal'); ?></p>
                        </div>

                        <label class="notifal-label" for="notifal_import_json"><?php esc_html_e('Exported JSON', 'notifal'); ?></label>
                        <textarea id="notifal_import_json"
                                  name="notifal_import_json"
                                  class="notifal-input notifal-import-json-textarea"
                                  rows="10"
                                  spellcheck="false"
                                  autocomplete="off"
                                  placeholder="<?php esc_attr_e('Paste your exported notification JSON here...', 'notifal'); ?>"></textarea>
                        <span class="notifal-help-text"><?php esc_html_e('Paste a single notification JSON export. ZIP bundles with media must be uploaded as a file.', 'notifal'); ?></span>

                        <label class="notifal-import-json-confirm">
                            <input type="checkbox"
                                   id="notifal_import_json_confirmed"
                                   name="notifal_import_json_confirmed"
                                   value="1">
                            <span><?php esc_html_e('I confirm this JSON is from a trusted source and I have reviewed it for safety.', 'notifal'); ?></span>
                        </label>
                    </div>
                </div>

                <div id="notifal-import-progress" class="notifal-progress notifal-hidden">
                    <div class="notifal-progress-bar">
                        <div class="notifal-progress-fill"></div>
                    </div>
                    <p class="notifal-progress-text"><?php esc_html_e('Importing...', 'notifal'); ?></p>
                </div>

                <div class="notifal-text-center notifal-text-sm notifal-text-muted notifal-mt-20">
                    <p><?php esc_html_e('Note: Imported notifications will be created as drafts. You can review and publish them after import.', 'notifal'); ?></p>
                </div>
            </form>
        </div>

        <div class="notifal-modal-footer">
            <button type="submit" class="notifal-button notifal-full-width" id="notifal-import-submit" form="notifal-import-form">
                <?php esc_html_e('Import OnPage Notifications', 'notifal'); ?>
            </button>
        </div>
    </div>
</div>

<?php
/**
 * Fires after rendering OnPage Notifications list admin page.
 *
 * Allows developers to add custom content or perform cleanup after the notifications list is rendered.
 *
 * @since 2.0.0
 * @param int $notification_total Total number of notifications
 * @param array $status_tabs Status tab data for filtering
 * @param string $add_new_url URL for creating new notifications
 */
do_action(ActionHooks::ADMIN_ONPAGE_NOTIFICATIONS_AFTER, $notification_total, $status_tabs, $add_new_url);
?>

<?php
// Render global toast container for dynamic notifications
ToastRenderer::renderGlobalContainer();
?> 
