-- Nay Store Admin Patch B - Operational Dashboard + Admin Activity Logs
-- Safe to run after Admin Patch A. It only ensures the activity log table/settings exist.

CREATE TABLE IF NOT EXISTS admin_activity_logs (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    admin_id INT UNSIGNED NULL,
    event_key VARCHAR(100) NOT NULL DEFAULT 'admin.action',
    action_label VARCHAR(190) NOT NULL DEFAULT '',
    target_type VARCHAR(80) NOT NULL DEFAULT '',
    target_id VARCHAR(80) NOT NULL DEFAULT '',
    description TEXT NULL,
    ip_address VARCHAR(64) NOT NULL DEFAULT '',
    payload_json LONGTEXT NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    KEY idx_admin_activity_admin (admin_id),
    KEY idx_admin_activity_event (event_key),
    KEY idx_admin_activity_created (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO settings (setting_key, setting_value, setting_group, created_at, updated_at)
VALUES
('admin_log_changes_enabled', '1', 'security', NOW(), NOW()),
('admin_track_ip_enabled', '1', 'security', NOW(), NOW())
ON DUPLICATE KEY UPDATE
setting_value = VALUES(setting_value),
setting_group = VALUES(setting_group),
updated_at = NOW();
