<?php
/**
 * HtaccessSchema — Database schema for the .htaccess Manager module.
 *
 * Tables owned:
 *   1. ultraguard_htaccess_change_log  — immutable audit trail of every file write
 *
 * All writes go through HtaccessAjax which calls ChangeLogTable::record() so
 * admins can review who changed the file, when, and what rule triggered the
 * change (save, apply_rules, import, reset, toggle_feature, etc.).
 *
 * @package UltraguardPro\modules\htaccess\schema
 */

namespace UltraguardPro\modules\htaccess\schema;

use UltraguardPro\modules\core\AbstractModuleSchema;

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

class HtaccessSchema extends AbstractModuleSchema {

	public function get_module_id(): string { return 'htaccess'; }

	// ── Table map ──────────────────────────────────────────────────────────────

	public function get_table_map(): array {
		return [
			'htaccess_change_log' => \UltraguardPro\modules\htaccess\db\HtaccessChangeLogTable::class,
		];
	}

	// ── CREATE TABLE statements ────────────────────────────────────────────────

	public function get_schemas(): array {
		$cs = $this->charset_collate();

		// IMPORTANT: dbDelta() does NOT support MySQL inline COMMENT clauses on
		// column definitions.  Any COMMENT '...' token causes dbDelta to mis-parse
		// the column list, producing broken ALTER TABLE SQL with empty column names
		// and NULL index keys.  Keep all CREATE TABLE SQL comment-free.
		return [
			$this->table( 'ultraguard_htaccess_change_log' ) =>
			"CREATE TABLE {$this->table('ultraguard_htaccess_change_log')} (
  id            bigint(20)   UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id       bigint(20)   UNSIGNED NOT NULL DEFAULT 0,
  user_login    varchar(100) NOT NULL DEFAULT '',
  change_reason varchar(100) NOT NULL DEFAULT 'manual_save',
  feature_key   varchar(100) DEFAULT NULL,
  file_size     int(11)      UNSIGNED NOT NULL DEFAULT 0,
  content_hash  char(64)     NOT NULL DEFAULT '',
  backup_path   varchar(512) DEFAULT NULL,
  ip_address    varchar(45)  NOT NULL DEFAULT '',
  created_at    datetime     NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY  (id),
  KEY idx_user_id   (user_id),
  KEY idx_reason    (change_reason),
  KEY idx_created   (created_at)
) {$cs};",
		];
	}
}
