<?php
/**
 * Create Review Sync Map Table Migration.
 *
 * @package Ugcify
 */

namespace Ugcify\Database\Migrations;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * CreateReviewSyncMapTable migration class.
 */
class CreateReviewSyncMapTable {

	/**
	 * WordPress database instance.
	 *
	 * @var \wpdb
	 */
	private $wpdb;

	/**
	 * Constructor.
	 *
	 * @param \wpdb $wpdb WordPress database instance.
	 */
	public function __construct( $wpdb ) {
		$this->wpdb = $wpdb;
	}

	/**
	 * Run migration.
	 *
	 * @return string SQL statement.
	 */
	public function run() {
		$table_name      = $this->wpdb->prefix . 'ugcify_review_sync_map';
		$charset_collate = $this->wpdb->get_charset_collate();

		return "CREATE TABLE $table_name (
			id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
			ugcify_review_id BIGINT(20) UNSIGNED DEFAULT NULL,
			wc_comment_id BIGINT(20) UNSIGNED DEFAULT NULL,
			source_of_truth VARCHAR(20) NOT NULL DEFAULT 'ugcify',
			last_synced_direction VARCHAR(20) DEFAULT NULL,
			ugcify_hash CHAR(32) DEFAULT NULL,
			wc_hash CHAR(32) DEFAULT NULL,
			last_synced_at DATETIME DEFAULT NULL,
			last_error LONGTEXT,
			created_at DATETIME NOT NULL,
			updated_at DATETIME NOT NULL,
			PRIMARY KEY (id),
			UNIQUE KEY ugcify_review_id_unique (ugcify_review_id),
			UNIQUE KEY wc_comment_id_unique (wc_comment_id),
			KEY source_of_truth (source_of_truth),
			KEY last_synced_direction (last_synced_direction),
			KEY last_synced_at (last_synced_at)
		) $charset_collate;";
	}
}
