<?php
// Add necessary cron functions
// Cron just needs to load the marketing resync functions and run it
function rentpress_cron_sync_data()
{
    $sync_index = get_option('rentpress_cron_start_incremental_data_sync_index');
    $sync_limit = 50;
    if ($sync_index === false) {
        require_once RENTPRESS_PLUGIN_DATA_SYNC . 'property/remote_data_refresh.php';
        rentpress_updateRefreshTable();
        update_option('rentpress_cron_start_incremental_data_sync_index', "zero");
        wp_schedule_single_event(time(), 'rentpress_cron_hook_sync_data');
    } else {
        if ($sync_index === "zero") {
            $sync_index = 0;
        }
        require_once RENTPRESS_PLUGIN_DATA_ACCESS . 'data_layer.php';
        require_once RENTPRESS_PLUGIN_DATA_SYNC . 'property/marketing_refresh.php';
        $refresh_properties = rentpress_getPaginatedRefreshData($sync_index, $sync_limit);
        if (empty($refresh_properties)) {
            delete_option('rentpress_cron_start_incremental_data_sync_index');
        } else {
            update_option('rentpress_cron_start_incremental_data_sync_index', $sync_index + $sync_limit);
            rentpress_syncFeedAndWPProperties($refresh_properties);
            wp_schedule_single_event(time(), 'rentpress_cron_hook_sync_data');
        }
    }
}
add_action('rentpress_cron_hook_sync_data', 'rentpress_cron_sync_data');

function rentpress_cron_start_data_sync()
{
    wp_schedule_single_event(time() + 10, 'rentpress_cron_hook_sync_data');
    wp_schedule_single_event(time() + 3600, 'rentpress_cron_hook_start_data_sync');
}
add_action('rentpress_cron_hook_start_data_sync', 'rentpress_cron_start_data_sync');

if (wp_next_scheduled('rentpress_cron_hook_start_data_sync') === false) {
    wp_schedule_single_event(time(), 'rentpress_cron_hook_start_data_sync');
}

////////////////////////////////////////////////
// daily sync for manual property data
///////////////////////////////////////////////

function rentpress_cron_sync_manual_data()
{
    require_once RENTPRESS_PLUGIN_DATA_SYNC . 'resync.php';
    require_once RENTPRESS_PLUGIN_ADMIN_POSTS . 'property/property_post_type_data.php';

    $property_posts = get_posts(array(
        'post_type' => 'rentpress_property',
        'post_status' => ['publish'],
        'posts_per_page' => -1,
    ));

    $rentpress_options = get_option('rentpress_options');
    $corresponding_meta_keys = rentpress_getCorrespondingWordpressToFeedKeyArray();
    $all_wordpress_meta = rentpress_getAllMetaDataForPropertiesAndFloorplansByPropertyPosts($property_posts);

    rentpress_saveManualWordpressData($all_wordpress_meta, $corresponding_meta_keys, $rentpress_options);
    rentpress_cron_sync_data();
}
add_action('rentpress_cron_hook_sync_manual_data', 'rentpress_cron_sync_manual_data');

function rentpress_cron_start_manual_data_sync()
{
    $time = new DateTime("today 24:00", new DateTimeZone(wp_timezone_string()));
    wp_schedule_single_event(time() + 60, 'rentpress_cron_hook_sync_manual_data');
    wp_schedule_single_event($time->getTimestamp(), 'rentpress_cron_hook_start_manual_data_sync');
}
add_action('rentpress_cron_hook_start_manual_data_sync', 'rentpress_cron_start_manual_data_sync');

if (wp_next_scheduled('rentpress_cron_hook_start_manual_data_sync') === false) {
    $time = new DateTime("today 24:00", new DateTimeZone(wp_timezone_string()));
    wp_schedule_single_event($time->getTimestamp(), 'rentpress_cron_hook_start_manual_data_sync');
}