<?php
/**
 * Database schema management for DataStream.
 *
 * @package DataStream
 */

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

/**
 * Class DataStream_Schema
 */
class DataStream_Schema {

	/**
	 * Create or update the ingestion table via dbDelta.
	 */
	public static function create_schema() {
		global $wpdb;

		$table           = $wpdb->prefix . DATASTREAM_TABLE;
		$charset_collate = $wpdb->get_charset_collate();

		$sql = "CREATE TABLE {$table} (
			id bigint(20) NOT NULL AUTO_INCREMENT,
			external_id varchar(100) NOT NULL,
			stream_type varchar(50) NOT NULL DEFAULT 'generic',
			source varchar(100) NOT NULL DEFAULT '',
			data_payload longtext NOT NULL,
			updated_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
			PRIMARY KEY  (id),
			UNIQUE KEY external_id (external_id),
			KEY stream_type (stream_type),
			KEY updated_at (updated_at)
		) {$charset_collate};";

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