<?php

namespace PWH_DCFH\App\Admin\Migration;

if (!defined('ABSPATH')) {
    exit;
}
if (!class_exists('PWH_DCFH_Updater')) {
    class PWH_DCFH_Updater
    {
        private static $_instance;

        /**
         * Returns the singleton instance of the class.
         * @return self
         */
        public static function instance()
        {
            if (self::$_instance === null) {
                self::$_instance = new self();
            }

            return self::$_instance;
        }

        /**
         * Initializer Of The Class
         * Add/Remove Necessary Actions/Filters
         */
        public function init()
        {
            add_action('init', [$this, 'maybe_migrate_options']);
        }

        /**
         * Migrate Options
         *
         * @return void
         */
        public function maybe_migrate_options()
        {
            if (false === get_option('_pwh_dcfh_options_updated')) {
                global $wpdb;
                $results = $wpdb->get_results($wpdb->prepare("SELECT DISTINCT option_name,option_value FROM $wpdb->options WHERE option_name LIKE %s", "_pwh_dcfh_contact_form_title_%")); //db call ok; no-cache ok
                if (!empty($results)) {
                    foreach ($results as $result) {
                        $contact_form_id = str_replace('_pwh_dcfh_contact_form_title_', '', $result->option_name);
                        $contact_form_title = $result->option_value;
                        $views = get_option('_pwh_dcfh_contact_form_views_'.$contact_form_id);
                        $unique_views = get_option('_pwh_dcfh_contact_form_unique_views_'.$contact_form_id);
                        $contact_form_views = false === $views ? 0 : $views;
                        $contact_form_unique_views = false === $unique_views ? 0 : $unique_views;
                        $contact_form_entries = pwh_dcfh_db()::get_contact_form_total_entries($contact_form_id);
                        $last_post_id = pwh_dcfh_db()::get_contact_form_last_post_id($contact_form_id);
                        $page_id = pwh_dcfh_post_meta_handler()::get_page_id_meta_value($last_post_id);
                        $option_key = "_pwh_dcfh_contact_form_settings_$contact_form_id";
                        $option = get_option($option_key);
                        if (false === $option) {
                            update_option($option_key, [
                                'unique_id' => $contact_form_id,
                                'page_id' => $page_id,
                                'title' => $contact_form_title,
                                'entries_threshold' => '-1',
                                'total_entries' => $contact_form_entries,
                                'views' => $contact_form_views,
                                'unique_views' => $contact_form_unique_views,
                            ], 'no');
                        }
                    }
                }
                update_option('_pwh_dcfh_options_updated', 'yes', 'no');
            }
        }
    }
}