<?php
/**
 * Bulk Processing View Template
 * 
 * Bulk operations dashboard for processing multiple products
 * 
 * @package AIProductContentImageGenerator
 * @since 1.0.0
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

// Get services
$apcig_ai_provider_manager = new \AIProductContentImageGenerator\Services\AIProviderManager();
$apcig_content_generator = new \AIProductContentImageGenerator\Services\ContentGenerator($apcig_ai_provider_manager);

// Get available options
$apcig_providers = $apcig_ai_provider_manager->get_configured_providers();
$apcig_tones = $apcig_content_generator->get_tones();
$apcig_content_types = $apcig_content_generator->get_content_types();

// Get all products for selection
$apcig_products = wc_get_products([
    'limit' => -1,
    'status' => 'publish',
    'orderby' => 'date',
    'order' => 'DESC'
]);

// Get product categories
$apcig_categories = get_terms([
    'taxonomy' => 'product_cat',
    'hide_empty' => false,
    'number' => 50
]);
?>

<div class="apcig-bulk-processing">
    <!-- Current Job Status Card -->
    <div class="apcig-card" id="current-job-status" style="display: none;">
        <div class="apcig-card-header">
            <h3><?php esc_html_e('Current Job Status', 'contengen-ai-product-content-image-generator'); ?></h3>
            <div class="apcig-job-controls">
                <button type="button" class="apcig-btn apcig-btn-warning apcig-btn-sm apcig-pause-bulk">
                    <span class="dashicons dashicons-controls-pause"></span>
                    <?php esc_html_e('Pause', 'contengen-ai-product-content-image-generator'); ?>
                </button>
                <button type="button" class="apcig-btn apcig-btn-danger apcig-btn-sm apcig-stop-bulk">
                    <span class="dashicons dashicons-controls-stop"></span>
                    <?php esc_html_e('Stop', 'contengen-ai-product-content-image-generator'); ?>
                </button>
            </div>
        </div>
        <div class="apcig-card-body">
            <div class="apcig-job-info">
                <div class="apcig-job-title">
                    <strong id="current-job-title"><?php esc_html_e('Processing: Content Generation', 'contengen-ai-product-content-image-generator'); ?></strong>
                </div>
                <div class="apcig-job-progress">
                    <div class="apcig-progress">
                        <div class="apcig-progress-bar" id="overall-progress-fill" style="width: 0%"></div>
                    </div>
                    <div class="apcig-progress-text">
                        <span id="overall-progress-text"><?php esc_html_e('0 of 0 completed', 'contengen-ai-product-content-image-generator'); ?></span>
                        <span id="estimated-time"><?php esc_html_e('Estimated time remaining: calculating...', 'contengen-ai-product-content-image-generator'); ?></span>
                    </div>
                </div>
                <div class="apcig-current-item">
                    <span id="current-item-text"><?php esc_html_e('Current Product: Loading...', 'contengen-ai-product-content-image-generator'); ?></span>
                </div>
            </div>
        </div>
    </div>

    <!-- Bulk Processing Setup -->
    <div class="apcig-bulk-setup">
        <div class="apcig-setup-grid">
            <!-- Product Selection Panel -->
            <div class="apcig-card">
                <div class="apcig-card-header">
                    <h3><?php esc_html_e('1. Select Products', 'contengen-ai-product-content-image-generator'); ?></h3>
                </div>
                <div class="apcig-card-body">
                    <!-- Selection Methods -->
                    <div class="apcig-selection-methods">
                        <div class="apcig-method-tabs">
                            <button type="button" class="apcig-method-tab active" data-method="manual">
                                <?php esc_html_e('Manual Selection', 'contengen-ai-product-content-image-generator'); ?>
                            </button>
                            <button type="button" class="apcig-method-tab" data-method="category">
                                <?php esc_html_e('By Category', 'contengen-ai-product-content-image-generator'); ?>
                            </button>
                            <button type="button" class="apcig-method-tab" data-method="filter">
                                <?php esc_html_e('By Filters', 'contengen-ai-product-content-image-generator'); ?>
                            </button>
                        </div>

                        <!-- Manual Selection -->
                        <div class="apcig-method-content active" id="manual-selection">
                            <div class="apcig-product-search">
                                <input type="text" id="product-search" class="apcig-input" placeholder="<?php esc_attr_e('Search products...', 'contengen-ai-product-content-image-generator'); ?>">
                            </div>
                            
                            <div class="apcig-selection-controls">
                                <button type="button" class="apcig-btn apcig-btn-secondary apcig-btn-sm apcig-select-all">
                                    <?php esc_html_e('Select All', 'contengen-ai-product-content-image-generator'); ?>
                                </button>
                                <button type="button" class="apcig-btn apcig-btn-secondary apcig-btn-sm apcig-select-none">
                                    <?php esc_html_e('Select None', 'contengen-ai-product-content-image-generator'); ?>
                                </button>
                                <button type="button" class="apcig-btn apcig-btn-secondary apcig-btn-sm apcig-select-missing-content">
                                    <?php esc_html_e('Missing Content', 'contengen-ai-product-content-image-generator'); ?>
                                </button>
                                <button type="button" class="apcig-btn apcig-btn-secondary apcig-btn-sm apcig-select-missing-images">
                                    <?php esc_html_e('Missing Images', 'contengen-ai-product-content-image-generator'); ?>
                                </button>
                            </div>

                            <div class="apcig-products-list" id="products-list">
                                <?php foreach ($apcig_products as $apcig_product): ?>
                                <div class="apcig-product-item" data-product-id="<?php echo esc_attr($apcig_product->get_id()); ?>">
                                    <label class="apcig-product-checkbox-label">
                                        <input type="checkbox" class="apcig-product-checkbox" value="<?php echo esc_attr($apcig_product->get_id()); ?>">
                                        <div class="apcig-product-info">
                                            <div class="apcig-product-image">
                                                <?php if ($apcig_product->get_image_id()): ?>
                                                    <?php echo wp_get_attachment_image($apcig_product->get_image_id(), 'thumbnail'); ?>
                                                <?php else: ?>
                                                    <div class="apcig-no-image">
                                                        <span class="dashicons dashicons-format-image"></span>
                                                    </div>
                                                <?php endif; ?>
                                            </div>
                                            <div class="apcig-product-details">
                                                <h4 class="apcig-product-title"><?php echo esc_html($apcig_product->get_name()); ?></h4>
                                                <div class="apcig-product-meta">
                                                    <span class="apcig-product-price"><?php echo wp_kses_post($apcig_product->get_price_html()); ?></span>
                                                    <?php if ($apcig_product->get_sku()): ?>
                                                        <span class="apcig-product-sku">SKU: <?php echo esc_html($apcig_product->get_sku()); ?></span>
                                                    <?php endif; ?>
                                                </div>
                                                <div class="apcig-content-status">
                                                    <span class="apcig-status-badge <?php echo esc_attr($apcig_product->get_short_description() ? 'success' : 'missing'); ?>">
                                                        <?php esc_html_e('Short Desc', 'contengen-ai-product-content-image-generator'); ?>
                                                    </span>
                                                    <span class="apcig-status-badge <?php echo esc_attr($apcig_product->get_description() ? 'success' : 'missing'); ?>">
                                                        <?php esc_html_e('Long Desc', 'contengen-ai-product-content-image-generator'); ?>
                                                    </span>
                                                    <?php
                                                    $apcig_tags = wp_get_post_terms($apcig_product->get_id(), 'product_tag');
                                                    ?>
                                                    <span class="apcig-status-badge <?php echo esc_attr(!empty($apcig_tags) ? 'success' : 'missing'); ?>">
                                                        <?php esc_html_e('Tags', 'contengen-ai-product-content-image-generator'); ?>
                                                    </span>
                                                </div>
                                            </div>
                                        </div>
                                    </label>
                                </div>
                                <?php endforeach; ?>
                            </div>
                        </div>

                        <!-- Category Selection -->
                        <div class="apcig-method-content" id="category-selection">
                            <div class="apcig-category-grid">
                                <?php foreach ($apcig_categories as $apcig_category): ?>
                                <label class="apcig-category-option">
                                    <input type="checkbox" name="categories[]" value="<?php echo esc_attr($apcig_category->term_id); ?>">
                                    <div class="apcig-category-info">
                                        <strong><?php echo esc_html($apcig_category->name); ?></strong>
                                        <?php /* translators: %d: number of products in this category */ ?>
                                        <small><?php echo esc_html(sprintf(_n('%d product', '%d products', $apcig_category->count, 'contengen-ai-product-content-image-generator'), $apcig_category->count)); ?></small>
                                    </div>
                                </label>
                                <?php endforeach; ?>
                            </div>
                        </div>

                        <!-- Filter Selection -->
                        <div class="apcig-method-content" id="filter-selection">
                            <div class="apcig-filter-options">
                                <div class="apcig-form-group">
                                    <label class="apcig-label"><?php esc_html_e('Product Status', 'contengen-ai-product-content-image-generator'); ?></label>
                                    <div class="apcig-checkbox-group">
                                        <label class="apcig-checkbox-option">
                                            <input type="checkbox" name="filters[]" value="missing-short-desc">
                                            <span><?php esc_html_e('Missing Short Description', 'contengen-ai-product-content-image-generator'); ?></span>
                                        </label>
                                        <label class="apcig-checkbox-option">
                                            <input type="checkbox" name="filters[]" value="missing-long-desc">
                                            <span><?php esc_html_e('Missing Long Description', 'contengen-ai-product-content-image-generator'); ?></span>
                                        </label>
                                        <label class="apcig-checkbox-option">
                                            <input type="checkbox" name="filters[]" value="missing-tags">
                                            <span><?php esc_html_e('Missing Tags', 'contengen-ai-product-content-image-generator'); ?></span>
                                        </label>
                                        <label class="apcig-checkbox-option">
                                            <input type="checkbox" name="filters[]" value="missing-images">
                                            <span><?php esc_html_e('Missing Images', 'contengen-ai-product-content-image-generator'); ?></span>
                                        </label>
                                    </div>
                                </div>

                                <div class="apcig-form-group">
                                    <label class="apcig-label"><?php esc_html_e('Price Range', 'contengen-ai-product-content-image-generator'); ?></label>
                                    <div class="apcig-price-range">
                                        <input type="number" name="price_min" class="apcig-input" placeholder="<?php esc_attr_e('Min price', 'contengen-ai-product-content-image-generator'); ?>">
                                        <span>-</span>
                                        <input type="number" name="price_max" class="apcig-input" placeholder="<?php esc_attr_e('Max price', 'contengen-ai-product-content-image-generator'); ?>">
                                    </div>
                                </div>

                                <div class="apcig-form-group">
                                    <label class="apcig-label"><?php esc_html_e('Date Range', 'contengen-ai-product-content-image-generator'); ?></label>
                                    <div class="apcig-date-range">
                                        <input type="date" name="date_from" class="apcig-input">
                                        <span>-</span>
                                        <input type="date" name="date_to" class="apcig-input">
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="apcig-selection-summary">
                        <strong><?php esc_html_e('Selected:', 'contengen-ai-product-content-image-generator'); ?> <span id="selected-count">0</span> <?php esc_html_e('products', 'contengen-ai-product-content-image-generator'); ?></strong>
                    </div>
                </div>
            </div>

            <!-- Action Configuration Panel -->
            <div class="apcig-card">
                <div class="apcig-card-header">
                    <h3><?php esc_html_e('2. Configure Action', 'contengen-ai-product-content-image-generator'); ?></h3>
                </div>
                <div class="apcig-card-body">
                    <!-- Action Type -->
                    <div class="apcig-form-group">
                        <label class="apcig-label"><?php esc_html_e('Bulk Action', 'contengen-ai-product-content-image-generator'); ?></label>
                        <div class="apcig-action-options">
                            <label class="apcig-radio-card">
                                <input type="radio" name="bulk_action" value="generate_content" checked>
                                <div class="apcig-radio-content">
                                    <span class="dashicons dashicons-edit-page"></span>
                                    <strong><?php esc_html_e('Generate Content', 'contengen-ai-product-content-image-generator'); ?></strong>
                                    <small><?php esc_html_e('Create descriptions, titles, and tags', 'contengen-ai-product-content-image-generator'); ?></small>
                                </div>
                            </label>
                            <label class="apcig-radio-card">
                                <input type="radio" name="bulk_action" value="generate_images">
                                <div class="apcig-radio-content">
                                    <span class="dashicons dashicons-format-image"></span>
                                    <strong><?php esc_html_e('Generate Images', 'contengen-ai-product-content-image-generator'); ?></strong>
                                    <small><?php esc_html_e('Create product images from descriptions', 'contengen-ai-product-content-image-generator'); ?></small>
                                </div>
                            </label>

                        </div>
                    </div>

                    <!-- Content Generation Settings -->
                    <div class="apcig-action-settings active" id="content-settings" style="display: block;">
                        <div class="apcig-form-group">
                            <label class="apcig-label"><?php esc_html_e('Content Types', 'contengen-ai-product-content-image-generator'); ?></label>
                            <div class="apcig-content-types">
                                <?php foreach ($apcig_content_types as $apcig_type_key => $apcig_type_config): ?>
                                <label class="apcig-checkbox-option">
                                    <input type="checkbox" name="content_types[]" value="<?php echo esc_attr($apcig_type_key); ?>" checked>
                                    <span><?php echo esc_html($apcig_type_config['name']); ?></span>
                                </label>
                                <?php endforeach; ?>
                            </div>
                        </div>

                        <div class="apcig-settings-row">
                            <div class="apcig-form-group">
                                <label class="apcig-label"><?php esc_html_e('AI Provider', 'contengen-ai-product-content-image-generator'); ?></label>
                                <select name="provider" class="apcig-select">
                                    <?php foreach ($apcig_providers as $apcig_provider_id => $apcig_provider_config): ?>
                                    <option value="<?php echo esc_attr($apcig_provider_id); ?>">
                                        <?php echo esc_html($apcig_provider_config['name']); ?>
                                    </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>

                            <div class="apcig-form-group">
                                <label class="apcig-label"><?php esc_html_e('Tone', 'contengen-ai-product-content-image-generator'); ?></label>
                                <select name="tone" class="apcig-select">
                                    <?php foreach ($apcig_tones as $apcig_tone_key => $apcig_tone_name): ?>
                                    <option value="<?php echo esc_attr($apcig_tone_key); ?>">
                                        <?php echo esc_html($apcig_tone_name); ?>
                                    </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>
                    </div>

                    <!-- Image Generation Settings -->
                    <div class="apcig-action-settings" id="image-settings" style="display: none;">
                        <div class="apcig-settings-row">
                            <div class="apcig-form-group">
                                <label class="apcig-label"><?php esc_html_e('Image Style', 'contengen-ai-product-content-image-generator'); ?></label>
                                <select name="image_style" class="apcig-select">
                                    <option value="clean-white"><?php esc_html_e('Clean White Background', 'contengen-ai-product-content-image-generator'); ?></option>
                                    <option value="studio"><?php esc_html_e('Studio Photography', 'contengen-ai-product-content-image-generator'); ?></option>
                                    <option value="lifestyle"><?php esc_html_e('Lifestyle Context', 'contengen-ai-product-content-image-generator'); ?></option>
                                </select>
                            </div>

                            <div class="apcig-form-group">
                                <label class="apcig-label"><?php esc_html_e('Image Size', 'contengen-ai-product-content-image-generator'); ?></label>
                                <select name="image_size" class="apcig-select">
                                    <option value="1024x1024"><?php esc_html_e('Square (1024x1024)', 'contengen-ai-product-content-image-generator'); ?></option>
                                    <option value="1024x768"><?php esc_html_e('Landscape (1024x768)', 'contengen-ai-product-content-image-generator'); ?></option>
                                    <option value="768x1024"><?php esc_html_e('Portrait (768x1024)', 'contengen-ai-product-content-image-generator'); ?></option>
                                </select>
                            </div>
                        </div>
                    </div>

                    <!-- Processing Options -->
                    <div class="apcig-processing-options">
                        <h4><?php esc_html_e('Processing Options', 'contengen-ai-product-content-image-generator'); ?></h4>
                        
                        <label class="apcig-checkbox-option">
                            <input type="checkbox" name="backup_original" id="backup-original" checked>
                            <span><?php esc_html_e('Backup original content before applying', 'contengen-ai-product-content-image-generator'); ?></span>
                        </label>
                        
                        <label class="apcig-checkbox-option">
                            <input type="checkbox" name="validate_content" id="validate-content" checked>
                            <span><?php esc_html_e('Validate generated content for quality', 'contengen-ai-product-content-image-generator'); ?></span>
                        </label>
                        
                        <label class="apcig-checkbox-option">
                            <input type="checkbox" name="skip_existing" id="skip-existing">
                            <span><?php esc_html_e('Skip products that already have content', 'contengen-ai-product-content-image-generator'); ?></span>
                        </label>
                        
                        <label class="apcig-checkbox-option">
                            <input type="checkbox" name="auto_apply" id="auto-apply">
                            <span><?php esc_html_e('Automatically apply generated content', 'contengen-ai-product-content-image-generator'); ?></span>
                        </label>
                    </div>

                    <!-- Batch Settings -->
                    <div class="apcig-batch-settings">
                        <h4><?php esc_html_e('Batch Settings', 'contengen-ai-product-content-image-generator'); ?></h4>
                        
                        <div class="apcig-settings-row">
                            <div class="apcig-form-group">
                                <label class="apcig-label"><?php esc_html_e('Batch Size', 'contengen-ai-product-content-image-generator'); ?></label>
                                <select name="batch_size" id="batch-size" class="apcig-select">
                                    <option value="5">5 <?php esc_html_e('products per batch', 'contengen-ai-product-content-image-generator'); ?></option>
                                    <option value="10" selected>10 <?php esc_html_e('products per batch', 'contengen-ai-product-content-image-generator'); ?></option>
                                    <option value="20">20 <?php esc_html_e('products per batch', 'contengen-ai-product-content-image-generator'); ?></option>
                                    <option value="50">50 <?php esc_html_e('products per batch', 'contengen-ai-product-content-image-generator'); ?></option>
                                </select>
                            </div>

                            <div class="apcig-form-group">
                                <label class="apcig-label"><?php esc_html_e('Delay Between Batches', 'contengen-ai-product-content-image-generator'); ?></label>
                                <select name="delay_between_batches" id="delay-between-batches" class="apcig-select">
                                    <option value="0"><?php esc_html_e('No delay', 'contengen-ai-product-content-image-generator'); ?></option>
                                    <option value="1">1 <?php esc_html_e('second', 'contengen-ai-product-content-image-generator'); ?></option>
                                    <option value="2" selected>2 <?php esc_html_e('seconds', 'contengen-ai-product-content-image-generator'); ?></option>
                                    <option value="5">5 <?php esc_html_e('seconds', 'contengen-ai-product-content-image-generator'); ?></option>
                                </select>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Start Processing Button -->
        <div class="apcig-start-processing">
            <button type="button" class="apcig-btn apcig-btn-primary apcig-btn-large apcig-start-bulk" disabled>
                <span class="dashicons dashicons-performance"></span>
                <?php esc_html_e('Start Bulk Processing', 'contengen-ai-product-content-image-generator'); ?>
            </button>
        </div>
    </div>

    <!-- Progress Card -->
    <div class="apcig-card" id="progress-card" style="display: none;">
        <div class="apcig-card-header">
            <h3><?php esc_html_e('Processing Progress', 'contengen-ai-product-content-image-generator'); ?></h3>
        </div>
        <div class="apcig-card-body">
            <div class="apcig-progress-layout">
                <!-- Progress Info -->
                <div class="apcig-progress-info">
                    <div class="apcig-progress-item">
                        <div class="apcig-progress">
                            <div class="apcig-progress-bar" id="batch-progress-fill" style="width: 0%"></div>
                        </div>
                        <div class="apcig-progress-label">
                            <span id="batch-progress-text"><?php esc_html_e('Preparing batch...', 'contengen-ai-product-content-image-generator'); ?></span>
                        </div>
                    </div>
                </div>

                <!-- Processing Log -->
                <div class="apcig-processing-log">
                    <h4><?php esc_html_e('Processing Log', 'contengen-ai-product-content-image-generator'); ?></h4>
                    <div class="apcig-log-container" id="processing-log">
                        <!-- Log entries will be added here -->
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Results Summary -->
    <div class="apcig-card" id="results-summary" style="display: none;">
        <div class="apcig-card-header">
            <h3><?php esc_html_e('Processing Complete', 'contengen-ai-product-content-image-generator'); ?></h3>
        </div>
        <div class="apcig-card-body">
            <div class="apcig-results-grid">
                <div class="apcig-result-item success">
                    <div class="apcig-result-number" id="successful-count">0</div>
                    <div class="apcig-result-label"><?php esc_html_e('Successful', 'contengen-ai-product-content-image-generator'); ?></div>
                </div>
                <div class="apcig-result-item error">
                    <div class="apcig-result-number" id="failed-count">0</div>
                    <div class="apcig-result-label"><?php esc_html_e('Failed', 'contengen-ai-product-content-image-generator'); ?></div>
                </div>
                <div class="apcig-result-item warning">
                    <div class="apcig-result-number" id="skipped-count">0</div>
                    <div class="apcig-result-label"><?php esc_html_e('Skipped', 'contengen-ai-product-content-image-generator'); ?></div>
                </div>
                <div class="apcig-result-item info">
                    <div class="apcig-result-number" id="total-time">0s</div>
                    <div class="apcig-result-label"><?php esc_html_e('Total Time', 'contengen-ai-product-content-image-generator'); ?></div>
                </div>
            </div>

            <div class="apcig-results-actions">
                <button type="button" class="apcig-btn apcig-btn-primary" onclick="location.reload()">
                    <?php esc_html_e('Start New Batch', 'contengen-ai-product-content-image-generator'); ?>
                </button>
                <button type="button" class="apcig-btn apcig-btn-secondary" id="export-results">
                    <?php esc_html_e('Export Results', 'contengen-ai-product-content-image-generator'); ?>
                </button>
            </div>
        </div>
    </div>

    <!-- Job Queue -->
    <div class="apcig-card">
        <div class="apcig-card-header">
            <h3><?php esc_html_e('Job Queue', 'contengen-ai-product-content-image-generator'); ?></h3>
            <button type="button" class="apcig-btn apcig-btn-secondary apcig-btn-sm" id="refresh-queue">
                <span class="dashicons dashicons-update"></span>
                <?php esc_html_e('Refresh', 'contengen-ai-product-content-image-generator'); ?>
            </button>
        </div>
        <div class="apcig-card-body">
            <div id="job-queue-list">
                <div class="apcig-queue-placeholder">
                    <span class="dashicons dashicons-clock"></span>
                    <p><?php esc_html_e('No jobs in queue', 'contengen-ai-product-content-image-generator'); ?></p>
                </div>
            </div>
        </div>
    </div>
</div>

