<?php
// Exit if accessed directly.
if (!defined('ABSPATH')) {
    exit;
}

/**
 *  @return array
 */
function pedro_demo_import(){

    $content_file    = dirname( plugin_dir_path( __FILE__ ) ) . '/demo/pedro-content.xml';
    $widget_file     = dirname( plugin_dir_path( __FILE__ ) ) . '/demo/pedro-widgets.wie';
    $customizer_file = dirname( plugin_dir_path( __FILE__ ) ) . '/demo/pedro-customizer.dat';

    if ( ! file_exists( $content_file ) ) {
        error_log('❌ Content file not found: ' . $content_file);
    }

    if ( ! file_exists( $widget_file ) ) {
        error_log('❌ Widget file not found: ' . $widget_file);
    }

    if ( ! file_exists( $customizer_file ) ) {
        error_log('❌ Customizer file not found: ' . $customizer_file);
    }

    return [
        [
            'import_file_name'             => 'Pedro Demo Import',
            'local_import_file'            => $content_file,
            'local_import_widget_file'     => $widget_file,
            'local_import_customizer_file' => $customizer_file,
            'import_preview_image_url'     => PDI_URL . '/demo/demo.png',
            'import_notice'                => __('Please wait, your demo content is being imported.', 'pedro-demo-import'),
        ],
    ];
}

add_filter('ocdi/import_files', 'pedro_demo_import');


/*========== images import =============*/
function pedro_demo_import_setup() {
    $home = get_page_by_title( 'Home' );
    $blog = get_page_by_title( 'Blog' );

    if ( $home ) {
        // Assign custom page template to Home page
        update_post_meta( $home->ID, '_wp_page_template', 'template-home.php' );

        update_option( 'show_on_front', 'page' );
        update_option( 'page_on_front', $home->ID );
    }

    if ( $blog ) {
        update_option( 'page_for_posts', $blog->ID );
    }

    // Assign menu (optional)
    $menu = wp_get_nav_menu_object( 'Primary Menu' );
    if ( $menu ) {
        $locations = get_theme_mod( 'nav_menu_locations' );
        if ( ! is_array( $locations ) ) {
            $locations = [];
        }
        $locations['primary_menu'] = $menu->term_id;
        set_theme_mod( 'nav_menu_locations', $locations );
    }
}
add_action( 'ocdi/after_import', 'pedro_demo_import_setup' );


