<?php
function smpro_install() {
	if (is_plugin_active('SocialTrafficBot/main.php')) {
		deactivate_plugins('SocialTrafficBot/main.php');
	}

	global $wpdb;
	$wpdb->show_errors();

	$table_name = $wpdb->prefix . "smpages";

	if ($wpdb->get_var("show tables like '$table_name'") != $table_name) {
		$sql = "CREATE TABLE $table_name (
		id int(11) NOT NULL AUTO_INCREMENT,
		node_id text,
		title text,
		watching text,
		talking_about int(11),
		likes int(11),
		keyword text NOT NULL,
		supressed text,
		UNIQUE KEY id (id)
		)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci AUTO_INCREMENT=1;";

		require_once ABSPATH . 'wp-admin/includes/upgrade.php';
		dbDelta($sql);
	}

	$table_name = $wpdb->prefix . "smhistory";

	if ($wpdb->get_var("show tables like '$table_name'") != $table_name) {
		$sql = "CREATE TABLE $table_name (
		ID INTEGER(11) UNSIGNED AUTO_INCREMENT,
		Post text,
		Comment text,
		Timesent int(11),
		Error text,
		campaign text,
		Source text,
		UNIQUE KEY id (id)
		)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci AUTO_INCREMENT=1;";

		require_once ABSPATH . 'wp-admin/includes/upgrade.php';
		dbDelta($sql);
	}

	$table_name = $wpdb->prefix . "smcampaigns";

	if ($wpdb->get_var("show tables like '$table_name'") != $table_name) {
		$sql = "CREATE TABLE $table_name (
		id INTEGER(11) UNSIGNED AUTO_INCREMENT,
		name text NOT NULL,
		status text NOT NULL,
		UNIQUE KEY id (id)
		)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci AUTO_INCREMENT=1;";

		require_once ABSPATH . 'wp-admin/includes/upgrade.php';
		dbDelta($sql);
	}

	$table_name = $wpdb->prefix . "smimages";

	if ($wpdb->get_var("show tables like '$table_name'") != $table_name) {
		$sql = "CREATE TABLE $table_name (
		id INTEGER(11) UNSIGNED AUTO_INCREMENT,
		username text,
		hashtags text,
		campaign INTEGER(11),
		UNIQUE KEY id (id)
		)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci AUTO_INCREMENT=1;";

		require_once ABSPATH . 'wp-admin/includes/upgrade.php';
		dbDelta($sql);
	}

	$table_name = $wpdb->prefix . "smfeeds";

	if ($wpdb->get_var("show tables like '$table_name'") != $table_name) {
		$sql = "CREATE TABLE $table_name (
		id INTEGER(11) UNSIGNED AUTO_INCREMENT,
		contentType varchar(50),
		hashtags text,
		campaign INTEGER(11),
		url text,
    titlesetId INTEGER(11),
    ioId INTEGER(11),
		UNIQUE KEY id (id)
		)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci AUTO_INCREMENT=1;";

		require_once ABSPATH . 'wp-admin/includes/upgrade.php';
		dbDelta($sql);
	}

	$table_name = $wpdb->prefix . "smtitlesets";

	if ($wpdb->get_var("show tables like '$table_name'") != $table_name) {
		$sql = "CREATE TABLE $table_name (
            id INTEGER(11) UNSIGNED AUTO_INCREMENT,
            setname varchar(50),
            title varchar(1024),
            UNIQUE KEY id (id)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci AUTO_INCREMENT=1;";

		require_once ABSPATH . 'wp-admin/includes/upgrade.php';
		dbDelta($sql);
	}

	$table_name = $wpdb->prefix . "smmasterlog";
	if ($wpdb->get_var("show tables like '$table_name'") != $table_name) {
		$sql = "CREATE TABLE $table_name (
		id INTEGER(11) UNSIGNED AUTO_INCREMENT,
		source varchar(200) not null,
		campaign_id integer(11) not null,
		uid varchar(200) not null,
		description varchar(1024),
		url varchar(1024) not null,
		created_at integer(11),
		post_id integer(11),
		error text,
		UNIQUE KEY id (id)
	 	)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci AUTO_INCREMENT=1;";

		require_once ABSPATH . 'wp-admin/includes/upgrade.php';
		dbDelta($sql);
	}

	$table_name = $wpdb->prefix . "smintrosoutros";
	if ($wpdb->get_var("show tables like '$table_name'") != $table_name) {
		$sql = "CREATE TABLE $table_name (
      id INTEGER(11) UNSIGNED AUTO_INCREMENT,
      setname varchar(50),
      intro varchar(1024),
      outro varchar(1024),
      UNIQUE KEY id (id)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci AUTO_INCREMENT=1;";

		require_once ABSPATH . 'wp-admin/includes/upgrade.php';
		dbDelta($sql);
	}

	$table_name = $wpdb->prefix . "smcustom";
   if ($wpdb->get_var("show tables like '$table_name'") != $table_name) {
    $sql = "CREATE TABLE $table_name (
	    id INTEGER(11) UNSIGNED AUTO_INCREMENT,
	    message text,
	    url varchar(2000),
	    headline varchar(1000),
	    image varchar(2000),
	    campaign INTEGER(11),
	    UNIQUE KEY id (id)
    )ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci AUTO_INCREMENT=1;";

    require_once ABSPATH . 'wp-admin/includes/upgrade.php';
    dbDelta($sql);
  }

	$table_name = $wpdb->prefix . "smreplies";

	if ($wpdb->get_var("show tables like '$table_name'") != $table_name) {
		$sql = "CREATE TABLE $table_name (
		id INTEGER(11) UNSIGNED AUTO_INCREMENT,
		Comment text,
		campaign text,
		UNIQUE KEY id (id)
		)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci AUTO_INCREMENT=1;";

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

	}
}
