<?php
/**
 * Live Feed Management Page.
 *
 * @package PrimeLink
 */

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

// Prepare global header context (same spacing/layout as other admin pages).
$header_actions     = '';
$header_eyebrow     = __( 'Live Feed', 'primelink' );
$header_title       = __( 'Real-time Product Synchronization', 'primelink' );
$header_description = __( 'Connect to remote WooCommerce stores and sync products in real-time using API keys.', 'primelink' );
?>

<div class="wrap mt-primelink-wrap mt_primelink-live-feed">
    <?php
    // Global admin header (consistent spacing with other pages).
    include MT_PRIMELINK_PATH . 'templates/admin/partials/header.php';
    ?>
    
    <div class="mt-primelink-live-feed-container">
        
        <!-- API Keys Section -->
        <div class="mt_primelink-card glass" style="margin-bottom:24px;">
            <div class="mt_primelink-card-header">
                <div>
                    <h2><?php esc_html_e( 'API Keys', 'primelink' ); ?></h2>
                    <p class="description"><?php esc_html_e( 'Generate API keys to allow other sites to access your product feed.', 'primelink' ); ?></p>
                </div>
                <button type="button" class="button button-primary" id="mt-primelink-create-api-key">
                    <?php esc_html_e( 'Generate API Key', 'primelink' ); ?>
                </button>
            </div>
            <div class="mt_primelink-card-body">
                <div id="mt-primelink-api-keys-list">
                    <p class="description"><?php esc_html_e( 'Loading API keys...', 'primelink' ); ?></p>
                </div>
            </div>
        </div>

        <!-- Feeds Section -->
        <div class="mt_primelink-card glass">
            <div class="mt_primelink-card-header">
                <div>
                    <h2><?php esc_html_e( 'Live Feeds', 'primelink' ); ?></h2>
                    <p class="description"><?php esc_html_e( 'Manage connections to remote WooCommerce stores for real-time product synchronization.', 'primelink' ); ?></p>
                </div>
                <button type="button" class="button button-primary" id="mt-primelink-add-feed">
                    <?php esc_html_e( 'Add Feed', 'primelink' ); ?>
                </button>
            </div>
            <div class="mt_primelink-card-body">
                <div id="mt-primelink-feeds-list">
                    <p class="description"><?php esc_html_e( 'Loading feeds...', 'primelink' ); ?></p>
                </div>
            </div>
        </div>

    </div>
</div>

<!-- API Key Modal -->
<div id="mt-primelink-api-key-modal" class="mt-primelink-modal" style="display:none;">
    <div class="mt-primelink-modal__overlay"></div>
    <div class="mt-primelink-modal__content glass">
        <div class="mt-primelink-modal__header">
            <h2><?php esc_html_e( 'Generate API Key', 'primelink' ); ?></h2>
            <button type="button" class="mt-primelink-modal__close">
                <span class="dashicons dashicons-no-alt"></span>
            </button>
        </div>
        <div class="mt-primelink-modal__body">
            <div class="mt_primelink-form-row">
                <label for="mt-primelink-api-key-name"><?php esc_html_e( 'Key Name', 'primelink' ); ?></label>
                <input type="text" id="mt-primelink-api-key-name" class="widefat" placeholder="<?php esc_attr_e( 'e.g., Main Store Feed', 'primelink' ); ?>" />
            </div>
            <div id="mt-primelink-api-key-result" style="display:none;margin-top:16px;padding:12px;background:#f0f0f0;border-radius:6px;">
                <p><strong><?php esc_html_e( 'API Key (save this - it won\'t be shown again):', 'primelink' ); ?></strong></p>
                <code id="mt-primelink-api-key-value" style="display:block;padding:8px;background:#fff;border:1px solid #ddd;border-radius:4px;word-break:break-all;"></code>
                <p class="description" style="margin-top:8px;">
                    <?php esc_html_e( 'Use this key in the header: X-API-Key: YOUR_KEY', 'primelink' ); ?>
                </p>
            </div>
        </div>
        <div class="mt-primelink-modal__footer">
            <button type="button" class="button mt-primelink-modal__close"><?php esc_html_e( 'Close', 'primelink' ); ?></button>
            <button type="button" class="button button-primary" id="mt-primelink-generate-api-key"><?php esc_html_e( 'Generate', 'primelink' ); ?></button>
        </div>
    </div>
</div>

<!-- Feed Modal -->
<div id="mt-primelink-feed-modal" class="mt-primelink-modal" style="display:none;">
    <div class="mt-primelink-modal__overlay"></div>
    <div class="mt-primelink-modal__content glass" style="max-width:600px;">
        <div class="mt-primelink-modal__header">
            <h2 id="mt-primelink-feed-modal-title"><?php esc_html_e( 'Add Live Feed', 'primelink' ); ?></h2>
            <button type="button" class="mt-primelink-modal__close">
                <span class="dashicons dashicons-no-alt"></span>
            </button>
        </div>
        <div class="mt-primelink-modal__body">
            <input type="hidden" id="mt-primelink-feed-id" />
            
            <div class="mt_primelink-form-row">
                <label for="mt-primelink-feed-name"><?php esc_html_e( 'Feed Name', 'primelink' ); ?></label>
                <input type="text" id="mt-primelink-feed-name" class="widefat" placeholder="<?php esc_attr_e( 'e.g., Main Store', 'primelink' ); ?>" />
            </div>

            <div class="mt_primelink-form-row">
                <label for="mt-primelink-feed-source-type"><?php esc_html_e( 'Source Type', 'primelink' ); ?></label>
                <select id="mt-primelink-feed-source-type" class="widefat">
                    <option value="prime_woocommerce"><?php esc_html_e( 'PrimeLink WooCommerce (PrimeLink on source store)', 'primelink' ); ?></option>
                    <option value="woocommerce_rest"><?php esc_html_e( 'WooCommerce REST API (no PrimeLink on source)', 'primelink' ); ?></option>
                </select>
                <p class="description">
                    <?php esc_html_e( 'Choose how the supplier store exposes its products: via PrimeLink Live Feed or via native WooCommerce REST API keys (Consumer key/secret).', 'primelink' ); ?>
                </p>
            </div>

            <div class="mt_primelink-form-row">
                <label for="mt-primelink-feed-source-url"><?php esc_html_e( 'Source URL', 'primelink' ); ?></label>
                <input type="url" id="mt-primelink-feed-source-url" class="widefat" placeholder="<?php esc_attr_e( 'https://example.com', 'primelink' ); ?>" />
                <p class="description"><?php esc_html_e( 'The URL of the WooCommerce store that will provide the feed.', 'primelink' ); ?></p>
            </div>

            <div class="mt_primelink-form-row" id="mt-primelink-feed-api-key-row">
                <label for="mt-primelink-feed-api-key"><?php esc_html_e( 'API Key', 'primelink' ); ?></label>
                <input type="text" id="mt-primelink-feed-api-key" class="widefat" placeholder="<?php esc_attr_e( 'Enter API key from source store', 'primelink' ); ?>" />
                <p class="description"><?php esc_html_e( 'The PrimeLink API key generated on the source store (Live Feed → Generate API Key).', 'primelink' ); ?></p>
            </div>

            <div class="mt_primelink-form-row" id="mt-primelink-feed-wc-keys-row" style="display:none;">
                <label><?php esc_html_e( 'WooCommerce REST API Keys', 'primelink' ); ?></label>
                <input type="text" id="mt-primelink-feed-wc-consumer-key" class="widefat" placeholder="<?php esc_attr_e( 'Consumer key (ck_...)', 'primelink' ); ?>" style="margin-bottom:8px;" />
                <input type="text" id="mt-primelink-feed-wc-consumer-secret" class="widefat" placeholder="<?php esc_attr_e( 'Consumer secret (cs_...)', 'primelink' ); ?>" />
                <p class="description">
                    <?php esc_html_e( 'These are the WooCommerce REST API credentials generated on the supplier store (WooCommerce → Settings → Advanced → REST API). Use Read-only keys.', 'primelink' ); ?>
                </p>
            </div>

            <div class="mt_primelink-form-row">
                <label for="mt-primelink-feed-sync-mode"><?php esc_html_e( 'Sync Mode', 'primelink' ); ?></label>
                <select id="mt-primelink-feed-sync-mode" class="widefat">
                    <option value="auto"><?php esc_html_e( 'Automatic (scheduled)', 'primelink' ); ?></option>
                    <option value="manual"><?php esc_html_e( 'Manual only', 'primelink' ); ?></option>
                </select>
            </div>

            <div class="mt_primelink-form-row" id="mt-primelink-feed-interval-wrap">
                <label for="mt-primelink-feed-interval"><?php esc_html_e( 'Sync Interval', 'primelink' ); ?></label>
                <select id="mt-primelink-feed-interval" class="widefat">
                    <option value="300"><?php esc_html_e( 'Every 5 minutes', 'primelink' ); ?></option>
                    <option value="900" selected><?php esc_html_e( 'Every 15 minutes', 'primelink' ); ?></option>
                    <option value="1800"><?php esc_html_e( 'Every 30 minutes', 'primelink' ); ?></option>
                    <option value="3600"><?php esc_html_e( 'Every hour', 'primelink' ); ?></option>
                    <option value="21600"><?php esc_html_e( 'Every 6 hours', 'primelink' ); ?></option>
                    <option value="86400"><?php esc_html_e( 'Once per day', 'primelink' ); ?></option>
                    <option value="custom"><?php esc_html_e( 'Custom (advanced)', 'primelink' ); ?></option>
                </select>
                <input type="number" id="mt-primelink-feed-interval-custom" class="widefat" style="display:none;margin-top:8px;" placeholder="<?php esc_attr_e( 'Enter seconds (minimum 60)', 'primelink' ); ?>" min="60" step="60" />
                <p class="description"><?php esc_html_e( 'How often to automatically synchronize products from the supplier store.', 'primelink' ); ?></p>
            </div>

            <div class="mt_primelink-form-row">
                <label><?php esc_html_e( 'Fields to synchronize', 'primelink' ); ?></label>
                <label><input type="checkbox" class="mt-primelink-sync-field" value="price" checked> <?php esc_html_e( 'Price', 'primelink' ); ?></label><br>
                <label><input type="checkbox" class="mt-primelink-sync-field" value="stock" checked> <?php esc_html_e( 'Stock', 'primelink' ); ?></label><br>
                <label><input type="checkbox" class="mt-primelink-sync-field" value="description" checked> <?php esc_html_e( 'Description (title & content)', 'primelink' ); ?></label><br>
                <label><input type="checkbox" class="mt-primelink-sync-field" value="images" checked> <?php esc_html_e( 'Images', 'primelink' ); ?></label><br>
                <label><input type="checkbox" class="mt-primelink-sync-field" value="categories" checked> <?php esc_html_e( 'Categories', 'primelink' ); ?></label><br>
                <label><input type="checkbox" class="mt-primelink-sync-field" value="tags" checked> <?php esc_html_e( 'Tags', 'primelink' ); ?></label>
                <p class="description">
                    <?php esc_html_e( 'Uncheck fields you do not want to be overwritten on existing products (for example, keep your own descriptions but sync price & stock).', 'primelink' ); ?>
                </p>
            </div>
        </div>
        <div class="mt-primelink-modal__footer">
            <button type="button" class="button mt-primelink-modal__close"><?php esc_html_e( 'Cancel', 'primelink' ); ?></button>
            <button type="button" class="button button-primary" id="mt-primelink-save-feed"><?php esc_html_e( 'Save Feed', 'primelink' ); ?></button>
        </div>
    </div>
</div>

