-- Migration: Create admin_logs table
-- This table tracks all important admin actions (edit, delete, modify, etc.)
-- Views are not logged, only actions that modify data

CREATE TABLE IF NOT EXISTS admin_logs (
  id INT AUTO_INCREMENT PRIMARY KEY,
  adminId INT NOT NULL,
  adminName VARCHAR(255) NOT NULL,
  adminEmail VARCHAR(255) NOT NULL,
  action VARCHAR(100) NOT NULL COMMENT 'Action type: edit, delete, create, update, modify, etc.',
  entityType VARCHAR(100) NOT NULL COMMENT 'Entity type: inquiry, supplier, user, order, category, settings, etc.',
  entityId INT NULL COMMENT 'ID of the affected entity',
  description TEXT NOT NULL COMMENT 'Human-readable description of the action',
  oldValue JSON NULL COMMENT 'Previous value (for edits/updates)',
  newValue JSON NULL COMMENT 'New value (for edits/updates)',
  ipAddress VARCHAR(45) NULL COMMENT 'IP address of the admin',
  userAgent TEXT NULL COMMENT 'User agent/browser info',
  createdAt DATETIME DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_admin_logs_admin (adminId),
  INDEX idx_admin_logs_entity (entityType, entityId),
  INDEX idx_admin_logs_action (action),
  INDEX idx_admin_logs_created (createdAt),
  FOREIGN KEY (adminId) REFERENCES users(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

