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

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

class LGBF_Activator {

	const DB_VERSION = '1.0';

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

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

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

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

	/**
	 * Plugin activation.
	 *
	 * @param bool $network_wide Whether activated network-wide.
	 */
	public static function activate( $network_wide = false ) {
		if ( is_multisite() && $network_wide ) {
			// Network activation across all sites is a Pro feature; on the
			// free plan we still activate cleanly on every site so nothing
			// breaks, but scanning runs per-site either way.
			$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 with the first scan
			// already queued — no wizard between "Activate" and value.
			LGBF_Scanner::request_immediate_scan();
			set_transient( 'lgbf_activation_redirect', 1, MINUTE_IN_SECONDS );
		}
	}

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

		add_option( 'lgbf_db_version', self::DB_VERSION );
		add_option( 'lgbf_scan_state', array() );
		add_option( 'lgbf_activated_at', time() );

		LGBF_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();
		$links_table     = self::links_table();
		$redirects_table = self::redirects_table();

		dbDelta(
			"CREATE TABLE {$links_table} (
				id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
				post_id bigint(20) unsigned NOT NULL DEFAULT 0,
				url text NOT NULL,
				url_hash char(32) NOT NULL DEFAULT '',
				anchor_text text,
				link_type varchar(10) NOT NULL DEFAULT 'link',
				status varchar(20) NOT NULL DEFAULT 'pending',
				http_status smallint(6) NOT NULL DEFAULT 0,
				redirect_count tinyint(3) unsigned NOT NULL DEFAULT 0,
				final_url text,
				check_count int(10) unsigned NOT NULL DEFAULT 0,
				last_checked datetime DEFAULT NULL,
				first_detected datetime DEFAULT NULL,
				created_at datetime DEFAULT NULL,
				PRIMARY KEY  (id),
				KEY url_hash (url_hash),
				KEY post_id (post_id),
				KEY status (status)
			) {$charset_collate};"
		);

		dbDelta(
			"CREATE TABLE {$redirects_table} (
				id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
				source_url text NOT NULL,
				source_hash char(32) NOT NULL DEFAULT '',
				target_url text NOT NULL,
				redirect_type smallint(6) NOT NULL DEFAULT 301,
				hits bigint(20) unsigned NOT NULL DEFAULT 0,
				is_active tinyint(1) NOT NULL DEFAULT 1,
				created_at datetime DEFAULT NULL,
				PRIMARY KEY  (id),
				UNIQUE KEY source_hash (source_hash)
			) {$charset_collate};"
		);
	}

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