<?php
/**
 * DBスキーマ管理 - 有効化時にテーブル作成
 *
 * @package CF7MasterPro\Database
 */

namespace CF7MasterPro\Database;

defined( 'ABSPATH' ) || exit;

class Schema {

	const VERSION_OPT = 'cf7mp_schema_version';

	public static function get_version() {
		return CF7MP_VERSION;
	}

	/**
	 * Activatorから呼び出し
	 */
	public static function install() {
		global $wpdb;
		require_once ABSPATH . 'wp-admin/includes/upgrade.php';

		$charset = $wpdb->get_charset_collate();
		$table   = $wpdb->prefix . CF7MP_DB_PREFIX . 'submissions';

		$sql = "CREATE TABLE {$table} (
			id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
			form_id BIGINT UNSIGNED NOT NULL DEFAULT 0,
			form_data LONGTEXT NULL,
			ip_address VARCHAR(64) NULL,
			user_agent VARCHAR(255) NULL,
			referer VARCHAR(255) NULL,
			utm_source VARCHAR(128) NULL,
			utm_medium VARCHAR(128) NULL,
			utm_campaign VARCHAR(128) NULL,
			priority VARCHAR(16) NULL,
			category VARCHAR(64) NULL,
			status VARCHAR(16) NOT NULL DEFAULT 'new',
			created_at DATETIME NOT NULL,
			PRIMARY KEY (id),
			KEY form_id (form_id),
			KEY status (status),
			KEY priority (priority),
			KEY category (category),
			KEY created_at (created_at)
		) {$charset};";

		dbDelta( $sql );
		update_option( self::VERSION_OPT, CF7MP_VERSION );
	}
}
