<?php

function create_site_poll_if_not_exists()
{
    //$blog_id = get_current_blog_id();
    //switch_to_blog( $blog_id );
    global $wpdb;
    $page_title = 'هیئت رئیسه';
    $page_slug  = 'heyat-raeis';
    $post_content = file_get_contents(__DIR__ . '/polls/heyat_raeis.json');

    $post = get_page_by_path($page_slug, OBJECT, 'poll');
    $status = "";

    if (!empty($post)) {
        $status = $post->post_status;
    }


    if (!get_page_by_path($page_slug, OBJECT, 'poll') && empty($post)) {
        // Page doesn’t exist, create it
        $new_page = array(
            'post_title'    => $page_title,
            'post_name'     => $page_slug,
            'post_content'  => '',
            'post_status'   => 'pending',
            'post_type'     => 'poll',
            'post_author'   => get_current_user_id(), // Change author ID if needed
        );

        $post_id = wp_insert_post($new_page);

        $post_content_array = json_decode($post_content, true);
        $post_content_array['id'] = $post_id;
        $post_content = json_encode($post_content_array, JSON_UNESCAPED_UNICODE);

        $query = $wpdb->prepare(
            "UPDATE {$wpdb->prefix}posts 
                 SET post_content = %s 
                 WHERE ID = %d",
            $post_content,
            $post_id
        );

        $result = $wpdb->query($query);
        if ($result === false) {
            error_log("Error: Failed to update post ID $post_id");
        } else {
            //error_log("Success: Updated post_content for ID $post_id");
        }
        $post = null;
    } elseif ($status === "draft" && !empty($post)) {

        $post_id = $post->ID;
        $post_content_array = json_decode($post_content, true);
        $post_content_array['id'] = $post_id;
        $post_content = json_encode($post_content_array, JSON_UNESCAPED_UNICODE);

        $query = $wpdb->prepare(
            "UPDATE {$wpdb->prefix}posts 
                 SET post_content = %s, post_status = %s 
                 WHERE ID = %d",
            $post_content,
            'pending',
            $post_id
        );

        $result = $wpdb->query($query);
        if ($result === false) {
            error_log("Error: Failed to update post ID $post_id");
        } else {
            //error_log("Success: Updated post_content for ID $post_id");
        }
        $post = null;
    }
    //error_log("FINAL QUERY: " . $query);
    echo  $page_title . '<br>';

    //////////////////////////////////////////////////
    $page_title = 'انتخابات شورای مرکزی';
    $page_slug  = 'shora-markazi';
    $post_content = file_get_contents(__DIR__ . '/polls/shora_markazi.json');

    $post = get_page_by_path($page_slug, OBJECT, 'poll');
    $status = "";

    if (!empty($post)) {
        $status = $post->post_status;
    }

    // Check if the page exists by title
    if (!get_page_by_path($page_slug, OBJECT, 'poll') && empty($post)) {

        // Page doesn’t exist, create it
        $new_page = array(
            'post_title'    => $page_title,
            'post_name'     => $page_slug,
            'post_content'  => '',
            'post_status'   => 'pending',
            'post_type'     => 'poll',
            'post_author'   => get_current_user_id(), // Change author ID if needed
        );

        $post_id = wp_insert_post($new_page);

        $post_content_array = json_decode($post_content, true);
        $post_content_array['id'] = $post_id;
        $post_content = json_encode($post_content_array, JSON_UNESCAPED_UNICODE);

        $query = $wpdb->prepare(
            "UPDATE {$wpdb->prefix}posts 
                 SET post_content = %s 
                 WHERE ID = %d",
            $post_content,
            $post_id
        );

        //error_log("FINAL QUERY: " . $query);
        $result = $wpdb->query($query);
        if ($result === false) {
            error_log("Error: Failed to update post ID $post_id");
        } else {
            //error_log("Success: Updated post_content for ID $post_id");
        }
    } elseif ($status === "draft" && !empty($post)) {

        $post_id = $post->ID;
        $post_content_array = json_decode($post_content, true);
        $post_content_array['id'] = $post_id;
        $post_content = json_encode($post_content_array, JSON_UNESCAPED_UNICODE);

        $query = $wpdb->prepare(
            "UPDATE {$wpdb->prefix}posts 
                 SET post_content = %s, post_status = %s 
                 WHERE ID = %d",
            $post_content,
            'pending',
            $post_id
        );

        $result = $wpdb->query($query);
        if ($result === false) {
            error_log("Error: Failed to update post ID $post_id");
        } else {
            //error_log("Success: Updated post_content for ID $post_id");
        }
        $post = null;
    }
    echo  $page_title . '<br>';
    /////////////////////////////////////////////////
    $page_title = 'انتخابات شورای بازرسی';
    $page_slug  = 'bazresi';
    $post_content = file_get_contents(__DIR__ . '/polls/bazresi.json');

    $post = get_page_by_path($page_slug, OBJECT, 'poll');
    $status = "";
    if (!empty($post)) {
        $status = $post->post_status;
    }

    // Check if the page exists by title
    if (!get_page_by_path($page_slug, OBJECT, 'poll') && empty($post)) {

        // Page doesn’t exist, create it
        $new_page = array(
            'post_title'    => $page_title,
            'post_name'     => $page_slug,
            'post_content'  => '',
            'post_status'   => 'pending',
            'post_type'     => 'poll',
            'post_author'   => get_current_user_id(), // Change author ID if needed
        );

        $post_id = wp_insert_post($new_page);
        $post_content_array = json_decode($post_content, true);
        $post_content_array['id'] = $post_id;
        $post_content = json_encode($post_content_array, JSON_UNESCAPED_UNICODE);

        $query = $wpdb->prepare(
            "UPDATE {$wpdb->prefix}posts 
                 SET post_content = %s 
                 WHERE ID = %d",
            $post_content,
            $post_id
        );

        //error_log("FINAL QUERY: " . $query);
        $result = $wpdb->query($query);
        if ($result === false) {
            error_log("Error: Failed to update post ID $post_id");
        } else {
            //error_log("Success: Updated post_content for ID $post_id");
        }
    } elseif ($status === "draft" && !empty($post)) {

        $post_id = $post->ID;
        $post_content_array = json_decode($post_content, true);
        $post_content_array['id'] = $post_id;
        $post_content = json_encode($post_content_array, JSON_UNESCAPED_UNICODE);

        $query = $wpdb->prepare(
            "UPDATE {$wpdb->prefix}posts 
                 SET post_content = %s, post_status = %s 
                 WHERE ID = %d",
            $post_content,
            'pending',
            $post_id
        );

        $result = $wpdb->query($query);
        if ($result === false) {
            error_log("Error: Failed to update post ID $post_id");
        } else {
            //error_log("Success: Updated post_content for ID $post_id");
        }
        $post = null;
    }
    echo  $page_title . '<br>';
    /////////////////////////////////////////////////
    $page_title = 'اساسنامه و مرامنامه';
    $page_slug  = 'rules-hezb';
    $post_content = file_get_contents(__DIR__ . '/polls/rules_hezb.json');

    $post = get_page_by_path($page_slug, OBJECT, 'poll');
    $status = "";
    if (!empty($post)) {
        $status = $post->post_status;
    }

    // Check if the page exists by title
    if (!get_page_by_path($page_slug, OBJECT, 'poll') && empty($post)) {
        // Page doesn’t exist, create it
        $new_page = array(
            'post_title'    => $page_title,
            'post_name'     => $page_slug,
            'post_content'  => '',
            'post_status'   => 'pending',
            'post_type'     => 'poll',
            'post_author'   => get_current_user_id(), // Change author ID if needed
        );

        $post_id = wp_insert_post($new_page);

        $post_content_array = json_decode($post_content, true);
        $post_content_array['id'] = $post_id;
        $post_content = json_encode($post_content_array, JSON_UNESCAPED_UNICODE);

        $query = $wpdb->prepare(
            "UPDATE {$wpdb->prefix}posts 
                 SET post_content = %s 
                 WHERE ID = %d",
            $post_content,
            $post_id
        );

        //error_log("FINAL QUERY: " . $query);
        $result = $wpdb->query($query);
        if ($result === false) {
            error_log("Error: Failed to update post ID $post_id");
        } else {
            //error_log("Success: Updated post_content for ID $post_id");
        }
    } elseif ($status === "draft" && !empty($post)) {

        $post_id = $post->ID;
        $post_content_array = json_decode($post_content, true);
        $post_content_array['id'] = $post_id;
        $post_content = json_encode($post_content_array, JSON_UNESCAPED_UNICODE);

        $query = $wpdb->prepare(
            "UPDATE {$wpdb->prefix}posts 
                 SET post_content = %s, post_status = %s 
                 WHERE ID = %d",
            $post_content,
            'pending',
            $post_id
        );

        $result = $wpdb->query($query);
        if ($result === false) {
            error_log("Error: Failed to update post ID $post_id");
        } else {
            //error_log("Success: Updated post_content for ID $post_id");
        }
        $post = null;
    }

    echo  $page_title . '<br>';
    // restore_current_blog();

    //فعال کردن ماژول های totalpoll
    //set option totalpoll_modules => a:1:{s:22:"media-contest-template";b:1;}
    // a:1:{i:0;s:22:"media-contest-template";}
    $totalpoll_modules = array('media-contest-template', 'b' => '1');
    update_option('totalpoll_modules', $totalpoll_modules);
}
