CREATE TABLE IF NOT EXISTS tenant_license_events (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    actor_tenant_id BIGINT UNSIGNED NULL,
    actor_user_id BIGINT UNSIGNED NULL,
    target_tenant_id BIGINT UNSIGNED NOT NULL,
    event_type VARCHAR(80) NOT NULL,
    payload JSON NULL,
    created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    INDEX idx_tenant_license_events_target (target_tenant_id, created_at),
    INDEX idx_tenant_license_events_type (event_type, created_at),
    CONSTRAINT fk_tenant_license_events_actor_tenant FOREIGN KEY (actor_tenant_id) REFERENCES tenants(id) ON DELETE SET NULL,
    CONSTRAINT fk_tenant_license_events_actor_user FOREIGN KEY (actor_user_id) REFERENCES users(id) ON DELETE SET NULL,
    CONSTRAINT fk_tenant_license_events_target_tenant FOREIGN KEY (target_tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
