<?php
/**
 * Activation / deactivation: tables, options, cron events.
 *
 * @package RedirectZen
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class RDZ_Activator {

	const DB_VERSION = '1.0';

	/**
	 * Redirects table name.
	 *
	 * @return string
	 */
	public static function redirects_table() {
		global $wpdb;

		return $wpdb->prefix . 'rdz_redirects';
	}

	/**
	 * 404 log table name.
	 *
	 * @return string
	 */
	public static function log_table() {
		global $wpdb;

		return $wpdb->prefix . 'rdz_404_log';
	}

	/**
	 * Backups table name.
	 *
	 * @return string
	 */
	public static function backups_table() {
		global $wpdb;

		return $wpdb->prefix . 'rdz_backups';
	}

	/**
	 * Plugin activation.
	 *
	 * @param bool $network_wide Whether activated network-wide.
	 */
	public static function activate( $network_wide = false ) {
		if ( is_multisite() && $network_wide ) {
			$site_ids = get_sites( array( 'fields' => 'ids', 'number' => 0 ) );
			foreach ( $site_ids as $site_id ) {
				switch_to_blog( $site_id );
				self::activate_single();
				restore_current_blog();
			}
		} else {
			self::activate_single();

			// Land the user straight on the dashboard — no wizard between
			// "Activate" and value.
			set_transient( 'rdz_activation_redirect', 1, MINUTE_IN_SECONDS );
		}
	}

	/**
	 * Per-site activation work.
	 */
	public static function activate_single() {
		self::create_tables();

		add_option( 'rdz_db_version', self::DB_VERSION );
		add_option( 'rdz_activated_at', time() );

		RDZ_Cron::schedule_events();
	}

	/**
	 * Creates (or upgrades) the custom tables.
	 */
	public static function create_tables() {
		global $wpdb;

		require_once ABSPATH . 'wp-admin/includes/upgrade.php';

		$charset_collate = $wpdb->get_charset_collate();
		$redirects_table = self::redirects_table();
		$log_table       = self::log_table();
		$backups_table   = self::backups_table();

		dbDelta(
			"CREATE TABLE {$redirects_table} (
				id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
				source text NOT NULL,
				source_hash char(32) NOT NULL DEFAULT '',
				target text NOT NULL,
				redirect_type smallint(6) NOT NULL DEFAULT 301,
				is_regex tinyint(1) NOT NULL DEFAULT 0,
				is_active tinyint(1) NOT NULL DEFAULT 1,
				hits bigint(20) unsigned NOT NULL DEFAULT 0,
				last_hit datetime DEFAULT NULL,
				verify_status varchar(20) NOT NULL DEFAULT 'pending',
				verify_http smallint(6) NOT NULL DEFAULT 0,
				verify_hops tinyint(3) unsigned NOT NULL DEFAULT 0,
				verify_final text,
				last_verified datetime DEFAULT NULL,
				created_at datetime DEFAULT NULL,
				updated_at datetime DEFAULT NULL,
				PRIMARY KEY  (id),
				UNIQUE KEY source_hash (source_hash),
				KEY is_active (is_active),
				KEY verify_status (verify_status)
			) {$charset_collate};"
		);

		dbDelta(
			"CREATE TABLE {$log_table} (
				id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
				path text NOT NULL,
				path_hash char(32) NOT NULL DEFAULT '',
				hits bigint(20) unsigned NOT NULL DEFAULT 1,
				referrer text,
				first_seen datetime DEFAULT NULL,
				last_seen datetime DEFAULT NULL,
				status tinyint(1) NOT NULL DEFAULT 0,
				PRIMARY KEY  (id),
				UNIQUE KEY path_hash (path_hash),
				KEY status (status),
				KEY last_seen (last_seen)
			) {$charset_collate};"
		);

		dbDelta(
			"CREATE TABLE {$backups_table} (
				id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
				label varchar(190) NOT NULL DEFAULT '',
				rows_count int(10) unsigned NOT NULL DEFAULT 0,
				data longtext,
				created_at datetime DEFAULT NULL,
				PRIMARY KEY  (id)
			) {$charset_collate};"
		);
	}

	/**
	 * Plugin deactivation: unschedule cron, keep data.
	 */
	public static function deactivate() {
		RDZ_Cron::clear_events();
	}
}
