<?php
/*
 * Example Usage for Story Importer
 * 
 * This file shows different ways to use the story importer on your other WordPress websites
 */

// Method 1: Include in your theme's functions.php
/*
require_once get_template_directory() . '/path/to/story-importer.php';

// Import stories on theme activation or when needed
function import_stories_on_activation() {
    $result = import_stories_from_source('https://kaveh.moeinwp.com/1', 10, 1);
    
    if ($result['success']) {
        error_log('Successfully imported ' . $result['imported'] . ' stories');
    } else {
        error_log('Story import failed: ' . $result['message']);
    }
}
*/

// Method 2: Create a custom admin page
/*
function add_custom_story_import_page() {
    add_menu_page(
        'Story Manager',
        'Story Manager', 
        'manage_options',
        'story-manager',
        'custom_story_import_page',
        'dashicons-format-gallery'
    );
}
add_action('admin_menu', 'add_custom_story_import_page');

function custom_story_import_page() {
    if (isset($_POST['import_now'])) {
        $result = import_stories_from_source('https://kaveh.moeinwp.com/1');
        echo '<div class="notice notice-success"><p>Import completed!</p></div>';
    }
    
    echo '<div class="wrap">';
    echo '<h1>Story Manager</h1>';
    echo '<form method="post">';
    echo '<input type="submit" name="import_now" value="Import Stories" class="button button-primary" />';
    echo '</form>';
    echo '</div>';
}
*/

// Method 3: Scheduled import using WordPress cron
/*
// Schedule import to run daily
function schedule_story_import() {
    if (!wp_next_scheduled('daily_story_import')) {
        wp_schedule_event(time(), 'daily', 'daily_story_import');
    }
}
add_action('wp', 'schedule_story_import');

// Handle the scheduled import
function handle_daily_story_import() {
    $result = import_stories_from_source('https://kaveh.moeinwp.com/1', 5, 1);
    
    // Log the result
    error_log('Daily story import: ' . json_encode($result));
    
    // Optionally send email notification
    if ($result['success'] && $result['imported'] > 0) {
        wp_mail(
            get_option('admin_email'),
            'Stories Imported Successfully',
            'Imported ' . $result['imported'] . ' new stories from source website.'
        );
    }
}
add_action('daily_story_import', 'handle_daily_story_import');
*/

// Method 4: Import specific stories by ID
/*
function import_specific_stories($story_ids) {
    $importer = new StoryImporter('https://kaveh.moeinwp.com/1');
    
    foreach ($story_ids as $story_id) {
        // Fetch specific story
        $response = wp_remote_get('https://kaveh.moeinwp.com/1/wp-json/wp/v2/story_shop/' . $story_id);
        
        if (!is_wp_error($response)) {
            $story_data = json_decode(wp_remote_retrieve_body($response), true);
            $result = $importer->import_single_story($story_data);
            
            if ($result['success']) {
                echo 'Imported story: ' . $story_data['title']['rendered'] . '<br>';
            }
        }
    }
}

// Usage: import_specific_stories([123, 456, 789]);
*/

// Method 5: Bulk import with pagination
/*
function bulk_import_all_stories($source_url) {
    $importer = new StoryImporter($source_url);
    $total_stories = $importer->get_total_stories();
    $per_page = 10;
    $total_pages = ceil($total_stories / $per_page);
    
    $total_imported = 0;
    
    for ($page = 1; $page <= $total_pages; $page++) {
        $result = $importer->import_stories($per_page, $page);
        
        if ($result['success']) {
            $total_imported += $result['imported'];
            echo 'Page ' . $page . ': Imported ' . $result['imported'] . ' stories<br>';
            
            // Add delay to prevent overwhelming the server
            sleep(2);
        } else {
            echo 'Page ' . $page . ': Failed - ' . $result['message'] . '<br>';
            break;
        }
    }
    
    echo 'Total imported: ' . $total_imported . ' out of ' . $total_stories . ' stories';
}

// Usage: bulk_import_all_stories('https://kaveh.moeinwp.com/1');
*/

// Method 6: Import with custom filtering
/*
function import_stories_with_filter($source_url, $filter_callback = null) {
    $importer = new StoryImporter($source_url);
    $stories = $importer->fetch_stories(100, 1); // Get more stories at once
    
    $imported = 0;
    
    foreach ($stories as $story) {
        // Apply custom filter if provided
        if ($filter_callback && !call_user_func($filter_callback, $story)) {
            continue; // Skip this story
        }
        
        $result = $importer->import_single_story($story);
        if ($result['success']) {
            $imported++;
        }
    }
    
    return $imported;
}

// Example filter: only import stories with badges
function only_badge_stories($story) {
    return isset($story['enable_badge']) && $story['enable_badge'] === true;
}

// Usage: import_stories_with_filter('https://kaveh.moeinwp.com/1', 'only_badge_stories');
*/

// Method 7: REST API endpoint for external triggers
/*
add_action('rest_api_init', function() {
    register_rest_route('custom/v1', '/import-stories', array(
        'methods' => 'POST',
        'callback' => 'rest_import_stories',
        'permission_callback' => function() {
            return current_user_can('manage_options');
        }
    ));
});

function rest_import_stories($request) {
    $source_url = $request->get_param('source_url') ?: 'https://kaveh.moeinwp.com/1';
    $per_page = $request->get_param('per_page') ?: 10;
    $page = $request->get_param('page') ?: 1;
    
    $result = import_stories_from_source($source_url, $per_page, $page);
    
    return new WP_REST_Response($result, 200);
}

// Usage: POST to /wp-json/custom/v1/import-stories
*/

// Method 8: WP-CLI command (if you use WP-CLI)
/*
if (defined('WP_CLI') && WP_CLI) {
    class Story_Import_Command {
        
        public function import($args, $assoc_args) {
            $source_url = $assoc_args['source'] ?? 'https://kaveh.moeinwp.com/1';
            $per_page = $assoc_args['per-page'] ?? 10;
            $page = $assoc_args['page'] ?? 1;
            
            WP_CLI::log('Starting story import from: ' . $source_url);
            
            $result = import_stories_from_source($source_url, $per_page, $page);
            
            if ($result['success']) {
                WP_CLI::success('Imported ' . $result['imported'] . ' stories');
            } else {
                WP_CLI::error('Import failed: ' . $result['message']);
            }
        }
    }
    
    WP_CLI::add_command('story import', 'Story_Import_Command');
}

// Usage: wp story import --source=https://kaveh.moeinwp.com/1 --per-page=20
*/

?>

<!-- HTML Usage Examples -->

<!-- Method 9: AJAX-powered admin interface -->
<script>
/*
jQuery(document).ready(function($) {
    $('#import-stories-btn').on('click', function() {
        var button = $(this);
        button.prop('disabled', true).text('Importing...');
        
        $.ajax({
            url: ajaxurl,
            type: 'POST',
            data: {
                action: 'import_stories',
                source_url: $('#source-url').val(),
                per_page: $('#per-page').val(),
                nonce: $('#story-import-nonce').val()
            },
            success: function(response) {
                if (response.success) {
                    alert('Successfully imported ' + response.imported + ' stories!');
                } else {
                    alert('Import failed: ' + response.message);
                }
            },
            complete: function() {
                button.prop('disabled', false).text('Import Stories');
            }
        });
    });
});
*/
</script>

<!-- 
HTML form for the AJAX example:

<div>
    <input type="url" id="source-url" value="https://kaveh.moeinwp.com/1" placeholder="Source URL" />
    <input type="number" id="per-page" value="10" min="1" max="100" />
    <button id="import-stories-btn">Import Stories</button>
    <input type="hidden" id="story-import-nonce" value="<?php echo wp_create_nonce('story_import_nonce'); ?>" />
</div>
-->