<?php
namespace WPVOFW;

/**
 * A utility class for managing the database and tables
 */
class DbTables{

	// 0.0.1 default
	static $db_version = '0.0.1';

    /**
	 * Runs on activation of the plugin.
	 *
	 * Checks if table exists and if not, creates it.
	 *
	 * @access public
	 * @return void
	 *
	 */
    public static function wpvofw_plugin_activated() {
		
		if ( get_option( 'wpvofw_db_version' ) !== self::$db_version ) {
			self::create_db_table();
		}
	}

    /**
	 * Creates the database tables.
	 *
	 * Runs on activation of the plugin.
	 * and creates the database tables.
	 *
	 * @access public
	 * @return void
	 *
	 */
    public static function create_db_table() {
        global $wpdb;

        $table_name = $wpdb->prefix . 'wpvofw_feedbacks';

        $wpdb_collate   =   $wpdb->collate;
        $query =    "CREATE TABLE {$table_name} (
                    `id` int(11) UNIQUE KEY AUTO_INCREMENT NOT NULL,
                    `order_id` bigint(20) NOT NULL,
                    `title` text NULL,
                    `message` text NULL,
                    `rating` float NULL,
					`rating_scale` int NULL,
                    `date_created` datetime NOT NULL,
                    `date_created_gmt` datetime NOT NULL,
                    `page_link` text NULL
                    ) collate {$wpdb_collate};";

		
		require_once ABSPATH . 'wp-admin/includes/upgrade.php';
		dbDelta( $query, true );

        $db_version_update = true;
        if ( $wpdb->get_var( "SHOW TABLES LIKE '{$table_name}'" ) === null ) {
			$db_version_update = false;
		}

        if ( $db_version_update ) {
			update_option( 'wpvofw_db_version', self::$db_version );
		} else {
			update_option( 'wpvofw_db_version', '0' );
		}
    }
}
?>